/*
--------------------------------------------------------------------------------
				Ak-Lib. 
			 Project       	: Ak-Lib
			 Group         	: Ak-Lib R&D Team
			 Author        	: Ocean.Wang
			 Create Date 	: 05-01-2010
--------------------------------------------------------------------------------
Code Name 	:  A.K 	~v~
Description	:  Yet Another Cross-Platform Lib.
Cautions    	:
--------------------------------------------------------------------------------
Change History:
Version		: 0.1
Building 		: 05/01/10
10-05.01 first draft, build, debug and test.
--------------------------------------------------------------------------------
*/

#include <fstream>
#include <iostream>
#include <iomanip>
#include <sstream>

#include <string>
#include <vector>
#include <map>

#include <sys/stat.h>

using namespace std;

/* ------------------------------------------------------------------
**	About XML
** 	Using the Tiny Xml
*/
#define TIXML_USE_STL
#include <algorithm>
#include "tinyxml.h"

class EdfXml{
public:
	EdfXml(string& _n_file){
		_m_file = _n_file;
		_m_root = NULL;
	};
	~EdfXml(){
	};

public:	
	int 		Load(){
		if(!_m_doc.LoadFile(_m_file))
			return -1;
		_m_root = _m_doc.RootElement();
		return 0;
	};
	
	int 		Save(){
		if(!_m_doc.SaveFile(_m_file))
			return -1;
		return 0;
	}

	int 		Create(string& _s_root, string& _s_comm){
		TiXmlDeclaration*pDecl	= new TiXmlDeclaration("1.0", "", "");
		TiXmlComment 	*pComm 	= new TiXmlComment();
		pComm->SetValue(_s_comm);

		_m_doc.LinkEndChild(pDecl);
		_m_doc.LinkEndChild(pComm);
		_m_root 	= new TiXmlElement(_s_root);
		_m_doc.LinkEndChild(_m_root);
		return Save();
	}

	TiXmlElement * AddElem(string& _s_elem){
		TiXmlElement 	*pElem = new TiXmlElement(_s_elem);
		_m_root->LinkEndChild(pElem);
		return pElem;
	}

	TiXmlElement * GetChild(TiXmlElement * pElem){
		TiXmlElement * pChild;
		pChild = NULL;
		if(pElem){
			pChild = pElem->FirstChildElement();
		}
		else if(_m_root){
			pChild = _m_root->FirstChildElement();
		}
		return pChild;
	}

	TiXmlElement * SetChild(TiXmlElement * pElem, string& _s_elem){
		TiXmlElement *pChild = new TiXmlElement(_s_elem);
		if(pElem){
			pElem->LinkEndChild(pChild);
		}
		else{
			_m_root->LinkEndChild(pChild);
		}
		return pChild;
	}
	
	TiXmlElement * GetNext(TiXmlElement * pElem){
		TiXmlElement * pNext;
		pNext = NULL;
		if(pElem){
			pNext = pElem->NextSiblingElement();
		}
		return pNext;
	}

	TiXmlElement * GetPrev(TiXmlElement * pElem){
		TiXmlNode 	 * pNode;
		TiXmlElement * pPrev;
		pPrev = NULL;
		if(pElem){
			pNode = pElem->PreviousSibling();
			if(pNode)
				pPrev = pNode->ToElement();
		}
		return pPrev;
	}

	const string* 	GetAttr(TiXmlElement * pElem, string& _s_attr){
		if(pElem){
			return pElem->Attribute(_s_attr);
		}
		return NULL;
	}
	
	const string* 	GetAttr(TiXmlElement * pElem, string& _s_attr, int * i){
		if(pElem){
			return pElem->Attribute(_s_attr, i);
		}
		return NULL;
	}
	
	void 	SetAttr(TiXmlElement * pElem, string& _s_attr, string& _s_val){
		if(pElem){
			return pElem->SetAttribute(_s_attr, _s_val);
		}
	}
	
	void 	SetAttr(TiXmlElement * pElem, string& _s_attr, int i){
		if(pElem){
			return pElem->SetAttribute(_s_attr, i);
		}
	}

	void 	ToUpper(string& _s_attr){
		transform(_s_attr.begin(), _s_attr.end(), _s_attr.begin(), toupper);	
	}

	void	ToLower(string& _s_attr){
		transform(_s_attr.begin(), _s_attr.end(), _s_attr.begin(), tolower); 
	}
	
private:	
	TiXmlDocument 	_m_doc;
	TiXmlElement *	_m_root;
	string 	_m_file;
};

/* ------------------------------------------------------------------
**	Class Declare.
*/
class 	Edf_Const;
class 	Edf_Type;
class 	Edf_Scope;
class	Edf_Prog;
class 	Edf_Enum;
class 	Edf_Union;
class 	Edf_Struct;
class 	Edf_Typedef;

#include "edf_types.h"

/* ------------------------------------------------------------------
**	About Type Enum
*/
#define SET_DECL_WIDTH 	4
#define EFID_MAX 		128



/* ------------------------------------------------------------------
**	About TLV 
**	Type 	:
** 	Length 	: unsigned int / 32 bit.
**	Value 	: 	 	
*/
class 	Edf_ConstVal{
public:
  	enum 	Edf_Const_ValType {
    	CV_SINT8,
    	CV_UINT8,
    	CV_SINT16,
    	CV_UINT16,
    	CV_SINT32,
    	CV_UINT32,
    	CV_SINT64,
    	CV_UINT64,
    	CV_DOUBLE,
    	CV_STRING,
  	};

  	Edf_ConstVal() {}
  	Edf_ConstVal(_sInt8 		val) {	valType  = CV_SINT8;		valSInt8 = val;  	}
  	Edf_ConstVal(_uInt8 		val) {	valType  = CV_UINT8;		valUInt8 = val;  	}
 	Edf_ConstVal(_sInt16  		val) {	valType  = CV_SINT16;		valSInt16 = val;  	}
  	Edf_ConstVal(_uInt16 		val) {	valType  = CV_UINT16;		valUInt16 = val;  	}
  	Edf_ConstVal(_sInt32  		val) {	valType  = CV_SINT32;		valSInt32 = val;  	}
  	Edf_ConstVal(_uInt32 		val) {	valType  = CV_UINT32;		valUInt32 = val;  	}
  	Edf_ConstVal(_sInt64   	val) {	valType  = CV_SINT64;		valSInt64 = val;  	}
  	Edf_ConstVal(_uInt64   	val) {	valType  = CV_UINT64;		valUInt64 = val;  	}
 	Edf_ConstVal(_dFloat		val) {	valType  = CV_DOUBLE;		valDouble = val;  	}
 	Edf_ConstVal(string 		val){	valType  = CV_STRING;		valString = val;  	}

	_sInt8 			get_sInt8()		{ return valSInt8; 	}
	_sInt16 		get_sInt16()	{ return valSInt16; }
	_sInt32 		get_sInt32()	{ return valSInt32; }
	_sInt64 		get_sInt64()	{ return valSInt64; }

	_uInt8			get_uInt8() 	{ return valUInt8;	}
	_uInt16 		get_uInt16()	{ return valUInt16; }
	_uInt32 		get_uInt32()	{ return valUInt32; }
	_uInt64 		get_uInt64()	{ return valUInt64; }

	_dFloat			get_double()	{ return valDouble; }
	string 			get_string()	{ return valString; }
	
private:
	Edf_Const_ValType 	valType;
	union{
		_sInt8 			valSInt8;
	  	_uInt8 			valUInt8;
	  	_sInt16 		valSInt16;
	  	_uInt16 		valUInt16;
		_sInt32			valSInt32;
		_uInt32 		valUInt32;
		_sInt64 		valSInt64;
		_uInt64 		valUInt64;
	};
	_dFloat 			valDouble;
  	string 				valString;
	
};


/* ------------------------------------------------------------------
**	Edf Const.
*/
class 	Edf_Const{
public:
  	Edf_Const(Edf_Type *type, string name, Edf_ConstVal	*value) :
    	_type(type), 	_name(name),   	_value(value) {}

  	Edf_Type* 		get_type() 	const {	return _type;  	}
  	string 			get_name() const {	return _name;  	}
 	Edf_ConstVal* 	get_value() const {	return _value;	}

private:
  	string 				_name;
  	Edf_Type			*_type;
  	Edf_ConstVal 		*_value;
};


/* ------------------------------------------------------------------
**	Edf Type.
*/
class Edf_Type {
public:
	Edf_Type()									:	_prog(NULL){	}
	Edf_Type(Edf_Prog* prog)						:	_prog(prog){	}
	Edf_Type(Edf_Prog* prog, string name) 		:	_prog(prog), _name(name){	}
	Edf_Type(string name)							:	_prog(NULL), _name(name){	}
  	virtual ~Edf_Type() {}

  	virtual void 				set_name(const string& name) 	{  	_name = name;  	}
  	virtual const string& 		get_name() const 				{  	return _name;  	}

	virtual bool is_void()		const 	{ return false; }
  	virtual bool is_basic() 		const 	{ return false; }
  	virtual bool is_string()    	const 	{ return false; }
  	virtual bool is_enum()     const 	{ return false; }
  	virtual bool is_union()     const 	{ return false; }
  	virtual bool is_struct()    	const 	{ return false; }
  	virtual bool is_typedef()   const 	{ return false; }
	virtual bool is_pack() 		const	{ return false;} 
	
  	Edf_Prog* 	get_prog() 		{  	return _prog; 	}

protected:
  	Edf_Prog* 		_prog;
  	string 			_name;
};

typedef enum ET_Baisc {
	TYPE_VOID,
	TYPE_S8,
	TYPE_S16,
	TYPE_S32,
	TYPE_S64,
	TYPE_U8,
	TYPE_U16,
	TYPE_U32,
	TYPE_U64,
	TYPE_DOUBLE,
	TYPE_STRING
}ET_Baisc;

class Edf_Basic : public Edf_Type {
public:

	Edf_Basic(ET_Baisc tb) : 
		Edf_Type(basic_name(tb)), _basic(tb), _is_strings(false), _is_binary(false), _is_enum(false){}

	ET_Baisc get_basic() const {return _basic;}

	bool is_void() 		const 	{return _basic == TYPE_VOID;}
	bool is_string() 	const 	{return _basic == TYPE_STRING;}
	
	void set_strings(bool val) 	{_is_strings = val;}
	bool is_strings() const 	{return (_basic == TYPE_STRING) && _is_strings;}

	void set_binary(bool val) 	{_is_binary = val;}
	bool is_binary() const 		{return (_basic == TYPE_STRING) && _is_binary; }

	void set_enum(bool val) 	{_is_enum = val;}
	bool is_enum() const 		{return _basic == TYPE_STRING && _is_enum;}
	void 						add_enum_val(string val) 		{_enum_vals.push_back(val);}
	const std::vector<string>& 	get_enum_vals() const 			{return _enum_vals;}

	bool is_basic() const 		{return true;}

	
	virtual string get_magic() const {
		string rv = basic_name(_basic);
		return rv;
	}
	
	static string basic_name(ET_Baisc tb) {
		switch (tb) {
		case TYPE_VOID   	: return    "void    "; break;
		case TYPE_S8   		: return 	"_sInt8  "; break;
		case TYPE_S16    	: return   	"_sInt16 "; break;
		case TYPE_S32    	: return   	"_sInt32 "; break;
		case TYPE_S64    	: return  	"_sInt64 "; break;
		case TYPE_U8   		: return 	"_uInt8  "; break;
		case TYPE_U16    	: return   	"_uInt16 "; break;
		case TYPE_U32    	: return   	"_uInt32 "; break;
		case TYPE_U64    	: return  	"_uInt64 "; break;
		case TYPE_DOUBLE 	: return  	"_dFloat "; break;
		case TYPE_STRING 	: return 	"_cSeq   "; break;
		default          	: return 	"void    "; break;
		}
	}

private:
	ET_Baisc 	_basic;
	bool 		_is_strings;
	bool 		_is_binary;
	bool 		_is_enum;
	std::vector<string> _enum_vals;
};

/* ------------------------------------------------------------------
**	Edf Enum.
*/
class Edf_EnumVal {
public:
  	Edf_EnumVal(string name) 				:  	_name(name),    _has_value(false),	_value(0) {}
  	Edf_EnumVal(string name, int value) 	:   _name(name),	_has_value(true),   _value(value) {}

  	const string& 		get_name() 		{ return _name;}
  	bool 				has_value() 		{ return _has_value;}
  	int 				get_value() 		{ return _value;}

private:
  	string 	_name;
  	bool 			_has_value;
  	int 			_value;
};


class Edf_Enum : public Edf_Type {
public:
  	Edf_Enum(Edf_Prog* prog)					 :    	Edf_Type(prog) {}
   	Edf_Enum(Edf_Prog* prog, const string& name) :    	Edf_Type(prog, name) {}

 	bool 								is_enum() const 					{ return true;	}

  	void 								set_name(const string& name) 	{ _name = name;  }
  	void 								append(Edf_EnumVal* constant) 		{ _constants.push_back(constant);	}
  	const std::vector<Edf_EnumVal*>& 	get_constants() 					{ return _constants; 	}

private:
	std::vector<Edf_EnumVal*> 		_constants;
};


/* ------------------------------------------------------------------
**	Edf Field
*/
class 	Edf_Field {
public:
  	Edf_Field(Edf_Type* type, string name) 								: _type(type), _name(name),_fid(0) , _value(NULL), _repeat(1){}
	Edf_Field(Edf_Type* type, string name, _uInt32 fid) 				: _type(type), _name(name),_fid(fid),_value(NULL), _repeat(1){}	
	Edf_Field(Edf_Type* type, string name, _uInt32 fid, _uInt32 repeat)	: _type(type), _name(name),_fid(fid),_value(NULL), _repeat(repeat){}	

  	Edf_Type* 			get_type() 	const 				{	return _type;}
  	const string& 		get_name()	const 				{ 	return _name;}
  	_uInt32 			get_fid()  	const 				{	return _fid;}
  	void 				set_value(Edf_ConstVal* value)	{	_value = value;}
  	Edf_ConstVal* 		get_value() 					{	return _value; }
	void 				set_repeat(_uInt32 repeat) 		{	_repeat = repeat;}
	_uInt32 			get_repeat()  					{	return _repeat;}
	bool 				is_sequence()const 			{ 	if(_repeat > 1) return true; else return false; }
  	enum 	Edf_Field_Req {
    	T_REQUIRED,
    	T_OPTIONAL,
  	};
  	void 				set_req(Edf_Field_Req req) 	{_req = req;}
  	Edf_Field_Req 		get_req() const 				{return _req;}


private:
  	Edf_Type* 		_type;
  	Edf_Field_Req 	_req;
  	Edf_ConstVal* 	_value;
  	string 			_name;
  	_uInt32 		_fid;
	_uInt32 		_repeat;
};


class Edf_Pack: public Edf_Type{
public:
	Edf_Pack(Edf_Prog* prog) 						:	Edf_Type(prog){}
	Edf_Pack(Edf_Prog* prog, const string& name) 	:	Edf_Type(prog, name){}
	void							set_name(const string& name)		{	_name = name;}
	void							append(Edf_Field* elem) 			{	_members.push_back(elem);}
	const std::vector<Edf_Field*>&	get_members()					{	return _members; }
	bool							validate_field(Edf_Field* field)		{
		int fid = field->get_fid();
		std::vector<Edf_Field*>::const_iterator m_iter;
		for (m_iter = _members.begin(); m_iter != _members.end(); ++m_iter) {
			if ((*m_iter)->get_fid() == fid) {
				return false;
			}
		}
		return true;
	}
	bool 							is_pack() const {return true;}

public:
	std::vector<Edf_Field*> 	_members;
	
};

/* ------------------------------------------------------------------
**	Edf Struct
*/
class Edf_Struct : public Edf_Pack {
public:
	Edf_Struct(Edf_Prog* prog) 						:	Edf_Pack(prog){}
	Edf_Struct(Edf_Prog* prog, const string& name) 	:	Edf_Pack(prog, name){}
	bool 							is_struct() const {return true;}
};


/* ------------------------------------------------------------------
**	Edf Union
*/
class Edf_Union : public Edf_Pack {
public:
	Edf_Union(Edf_Prog* prog) 						:	Edf_Pack(prog){}
	Edf_Union(Edf_Prog* prog, const string& name) 	:	Edf_Pack(prog, name){}
	bool 							is_union() const {return true;}
};


/* ------------------------------------------------------------------
**	Edf Typedef 
*/
class Edf_Typedef : public Edf_Type {
public:
  	Edf_Typedef(Edf_Prog* prog, Edf_Type* type, string symbol) :	Edf_Type(prog, symbol), _type(type), _symbol(symbol) {}
  	bool 				is_typedef() 	const {	return true;	}

  	Edf_Type* 			get_type() 		const { return _type; 	}
  	const string& 		get_symbol() 	const { return _symbol;	}


private:
  	Edf_Type* 		_type;
  	string 	_symbol;
};


/* ------------------------------------------------------------------
**	Edf Scope
** 	Looking up Scope for: Constant / Typedef.
*/
class 	Edf_Scope {
public:
  	Edf_Scope() {}

  	void 		add_type(string name, Edf_Type* type) 				{ 	types[name] = type;  		}
  	Edf_Type* 	get_type(string name) 								{	return types[name];  		}
  	void 		add_constant(string name, Edf_Const* constant) 	{ 	constants[name] = constant;}
  	Edf_Const* 	get_constant(string name) 							{  	return constants[name];  	}

  	void 		print() {
    	std::map<string, Edf_Type*>::iterator iter;
    	for (iter = types.begin(); iter != types.end(); ++iter) {
      		printf("%s => %s\n", 	
				iter->first.c_str(), 	
				iter->second->get_name().c_str());
    	}
  	}

private:
  	std::map<string, Edf_Type *> types;
  	std::map<string, Edf_Const*> constants;
};


/* ------------------------------------------------------------------
**	Edf Program.
** 	<Include>  
**	<Namespace>
**	<Name>
**	<Input>
**	<Output>
**	<Enum>
**	<Union>
**	<Struct>
**	<Typedef>
*/
class Edf_Prog{
 public:
	Edf_Prog(string path, string name) 	:	_path(path), _output("./"), _name(name){ 				_scope = new Edf_Scope();	}
	Edf_Prog(string path) 				:	_path(path), _output("./") {_name  = prog_name(path); 	_scope = new Edf_Scope(); }

  	const string& get_path() 				const { return _path; }
  	const string& get_output() 			const { return _output; }
  	const string& get_name() 			const { return _name; }
 	const string& get_namespace() 		const { return _namespace; }
  	const string& get_prefix() 			const { return _prefix; }

  	const std::vector<Edf_Typedef*>& 	get_typedefs()  	const { return _typedefs;  }
  	const std::vector<Edf_Const*>&   	get_consts()   	const { return _consts;    }
  	const std::vector<Edf_Enum*>&    	get_enums()     	const { return _enums;     }
  	const std::vector<Edf_Union*>&    	get_unions()    	const { return _unions;    }
  	const std::vector<Edf_Struct*>&  	geEdf_Structs() 	const { return _structs;   }

  	void add_typedef  (Edf_Typedef* td) { _typedefs.push_back(td);  	}
  	void add_const     (Edf_Const*   tc) { _consts.push_back(tc);    	}
  	void add_enum     (Edf_Enum*    te) { _enums.push_back(te);     	}
  	void add_union    (Edf_Union*   tu) { _unions.push_back(tu);    	}
  	void add_struct   (Edf_Struct*  ts) { _structs.push_back(ts);		}
  	void add_pack   (Edf_Pack*  tp) 	{ _packs.push_back(tp);		}

  	const std::vector<Edf_Prog*>& get_includes() const { return _includes; }

	string 	prog_name(string path) {
	  	string::size_type slash = path.rfind("/");
	  	if (slash != string::npos) {
	    	path = path.substr(slash+1);
	  	}
	  	string::size_type dot = path.rfind(".");
	  	if (dot != string::npos) {
	    	path = path.substr(0, dot);
	  	}
	  	return path;
	}

 	void set_out_path(string out_path) {
    	_output = out_path;
    	char c = _output.at(_output.size() - 1);
    	if (!(c == '/' || c == '\\')) {
      		_output.push_back('/');
    	}
  	}

  	void set_namespace(string name) {
    	_namespace = name;
  	}

  	Edf_Scope* scope() {
    	return _scope;
  	}

  	void add_include(string path, string include) {
    	Edf_Prog* prog = new Edf_Prog(path);

    	string 			include_prefix;
    	string::size_type 	slash = string::npos;
    	if ((slash = include.rfind("/")) != string::npos) {
      		include_prefix = include.substr(0, slash);
    	}

    	prog->set_prefix(include_prefix);
    	_includes.push_back(prog);
  	}

  	std::vector<Edf_Prog*>& get_includes() {
   	 return _includes;
  	}

  	void set_prefix(string include_prefix) {
    	_prefix = include_prefix;
    	int len = (int)(_prefix.size());
    	if (len > 0 && _prefix[len - 1] != '/') {
      		_prefix += '/';
    	}
  	}

  	void set_namespace(string language, string name_space) {
    	_namespaces[language] = name_space;
  	}

  	string get_namespace(string language) const {
    	std::map<string, string>::const_iterator iter = _namespaces.find(language);
    	if (iter == _namespaces.end()) {
      		return string();
    	}
    	return iter->second;
  	}

private:
  	string 						_path;
  	string 						_name;
  	string 						_output;

  	Edf_Scope					*_scope;
  	std::vector<Edf_Prog*> 		_includes;
  	string 						_prefix;

  	std::vector<Edf_Const*>   	_consts;
  	std::vector<Edf_Union*>   	_unions;
  	std::vector<Edf_Struct*>  	_structs;
  	std::vector<Edf_Typedef*> 	_typedefs;

	
  	string 						_namespace;
  	std::map<string, string> 	_namespaces;

public:
  	std::vector<Edf_Enum*>    	_enums;
	std::vector<Edf_Pack*> 		_packs;

	Edf_Type *					TypeLookup(string & name)		{

	std::vector<Edf_Enum*>::const_iterator e_iter;
	for (e_iter = _enums.begin(); e_iter != _enums.end(); ++e_iter) {
		if ((*e_iter)->get_name().compare(name) == 0) {
			return (Edf_Type *)(*e_iter);
		}
	}
	
	std::vector<Edf_Pack*>::const_iterator p_iter;
	for (p_iter = _packs.begin(); p_iter != _packs.end(); ++p_iter) {
		if ((*p_iter)->get_name().compare(name) == 0) {
			return (Edf_Type *)(*p_iter);
		}
	}
	
	return NULL;
}

};


/* ------------------------------------------------------------------
**	Edf Generator.
*/
class 	Edf_Generator {
public:
	Edf_Generator(Edf_Prog* program) {
		_tmp 	= 0;
		_indent = 0;
		_prog 	= program;
		_name 	= get_prog_name(program);
	}

	virtual ~Edf_Generator() {}

	#if 0
	virtual void 	gen_program();
	#endif
	const Edf_Prog* get_prog() const { return _prog; }

protected:
	virtual void generator_init() {}
	virtual void generator_fini() {}
	virtual void gen_const(Edf_Const		*tc) {}
	virtual void gen_consts(std::vector<Edf_Const*> consts)=0;
	virtual void gen_enum(Edf_Enum			*te)	= 0;
	virtual void gen_pack(Edf_Pack		*tp)   	= 0;

	virtual void gen_typedef(Edf_Typedef	*tt)  	= 0;

	virtual string get_prog_name(Edf_Prog* prog) {
		return prog->get_name();
	}

	virtual string get_output() const {
		return _prog->get_output() + _output + "/";
	}

	string tmp(string name) {
		std::ostringstream out;
		out << name;
		return out.str();
	}

	/* Indent */
	void 		indent_up()		{++_indent;	}
	void 		indent_down() 	{--_indent;	}
	string indent() 		{
		string ind = "";
		int i;
		for (i = 0; i < _indent; ++i) {
			ind += "\t";
		}
		return ind;
	}

	std::ostream& indent(std::ostream &os) {return os << indent();}

	/* Scope */
	void 	scope_up(std::ostream& out) 	{	indent(out) << "{" << std::endl;	indent_up();	}
	void 	scope_down(std::ostream& out) 	{	indent_down();	indent(out) << "}" << std::endl;	}

	/* Comment */
	virtual string comment() {
		return		string("/*\n") + "** Generate By <Exchange Data Format>\n" +	"*/\n";
	}

	string capitalize(string in) {
		in[0] = toupper(in[0]);
		return in;
	}
	
	string decapitalize(string in) {
		in[0] = tolower(in[0]);
		return in;
	}
	
	string lowercase(string in) {
		for (size_t i = 0; i < in.size(); ++i) {
			in[i] = tolower(in[i]);
		}
		return in;
	}

	static Edf_Type* get_true_type(Edf_Type* type) {
		while (type->is_typedef()) {
			type = ((Edf_Typedef*)type)->get_type();
		}
		return type;
	}

protected:
	Edf_Prog		*_prog;
	string 	_name;
	string 	_output;
	string 	_dec_unknown;
	
private:
	int 			_indent;
	int 			_tmp;

};


/* ------------------------------------------------------------------
**	Edf C Generator.
*/
class Edf_GenerC : public Edf_Generator {
public:
	Edf_GenerC(Edf_Prog* 							program,
				const std::map<string, string>& 	options,
				const string& 						option)
		: Edf_Generator(program){

		std::map<string, string>::const_iterator iter;
		iter = options.find("inc_prefix");
		_use_inc_prefix = (iter != options.end());
		_output = "gen-c";
	}

	void 	generator_init();
	void 	generator_fini();

	void 	gen_consts(std::vector<Edf_Const*> consts);
	void 	gen_typedef(Edf_Typedef* tt);
	void 	gen_enum(		Edf_Enum	*te);
	
	void 	gen_pack(Edf_Pack	*tp) {
		string 	tn;
		Edf_Type * 	tf;
		
		tn = tp->get_name();
		tf = get_true_type(tp);
		
		_vg_node = tn;
		_vg_clue.clear();

		gen_pack_def(_f_types, tp);
		gen_pack_decl(_f_types, tp);

		gen_pack_def_if(_f_types, tp);
		gen_pack_decl_if(_f_types, tp);

		if(tf->is_union()){
			gen_pack_clean(_f_types_impl, tp, true, false);
		}
		
		
		gen_pack_impl_if(_f_types_impl, tp, false, false);
		gen_pack_ftbl(_f_types_impl, tp, false, false);

		gen_pack_init(_f_types_impl, tp, false, false);
		gen_pack_encode(_f_types_impl, tp, false, false);
		gen_pack_decode(_f_types_impl, tp, false, false);
		gen_pack_sizeof(_f_types_impl, tp, false, false);

		gen_pack_var_if(_f_types_impl, tp, false, false);
		gen_pack_init_if(_f_types_impl, tp, false, false);
	}

	void 		const_value_print(	std::ofstream& out, string name, Edf_Type* type, Edf_ConstVal* value);
	string 		const_value_render(	std::ofstream& out, string name, Edf_Type* type, Edf_ConstVal* value);


	/* Utils */
	bool 		is_complex(Edf_Type* tt);
	_sInt32 	get_simple_len(Edf_Type * tt);
	int			get_type_id(Edf_Type* tt);

	string 		get_typeenum(Edf_Type* tt);
	string 		get_typename(Edf_Type* tt, bool is_typedef=false, bool arg=false);
	string 		get_typename_basic(ET_Baisc tb);
	string 		get_inc_prefix(const Edf_Prog& prog);
	void 		set_use_inc_prefix(bool use_inc_prefix) 	{	_use_inc_prefix = use_inc_prefix;}

	/* Endian */
	void		gen_hton(ofstream& out, Edf_Type * tt);
	void		gen_ntoh(ofstream& out, Edf_Type * tt);

	/* Define */
	void 		gen_unknown(ofstream& out, string& sModule);
	void 		gen_comment(ofstream& out, string& sComment);
	
	string 		gen_field_init(Edf_Field* tf);
	void 		gen_field_decl(ofstream& out, Edf_Field* tf, bool pointer=false, bool constant=false, int repeat=1);
	void 		gen_field_presence(ofstream& out, Edf_Field* tf, int repeat=1);
	void 		gen_field_def_if(ofstream& out, Edf_Type * tt, Edf_Field* tf, bool pointer);
	void 		gen_field_var_if(ofstream& out, Edf_Type * tt, Edf_Field* tf, bool pointer);
	void 		gen_field_impl_if(ofstream& out, Edf_Type * tt, Edf_Field* tf);


	/* Declare Interface */
	void 		gen_pack_def(ofstream& out, Edf_Pack* tp, bool is_ref=false, bool is_pointer=false);
	void 		gen_pack_decl(ofstream& out, Edf_Pack* tp, bool is_ref=false, bool is_pointer=false);
	void 		gen_pack_def_if(ofstream& out, Edf_Pack* tp, 	bool is_ref=false, bool is_pointer=false);
	void 		gen_pack_decl_if(ofstream& out,Edf_Pack* tp, 	bool is_ref=false, bool is_pointer=false);
	void 		gen_pack_var_if(ofstream& out, Edf_Pack* tp, 	bool is_ref=false, bool is_pointer=false);
	void		gen_pack_init_if(ofstream& out, Edf_Pack* tp,   bool is_ref=false, bool is_pointer=false);
	void 		gen_pack_impl_if(ofstream& out, Edf_Pack* tp, bool is_ref=false, bool is_pointer=false);

	/* Simple */
	void		gen_set_simple(ofstream& out, Edf_Type * tt, Edf_Field * tf, bool unk=false);
	void		gen_exist_simple(ofstream& out, Edf_Type * tt, Edf_Field * tf, bool unk=false);
	void		gen_clear_simple(ofstream& out, Edf_Type * tt, Edf_Field * tf, bool unk=false);
	void		gen_get_simple(ofstream& out, Edf_Type * tt, Edf_Field * tf, bool unk=false);
	void		gen_enc_simple(ofstream& out, Edf_Type * tt, Edf_Field * tf, bool unk=false);
	void		gen_size_simple(ofstream& out, Edf_Type * tt, Edf_Field * tf, bool unk=false);
	void 		gen_dec_simple(ofstream& out, Edf_Type * tt, Edf_Field * tf, bool unk=false);
	void		gen_method_simple(ofstream& out, Edf_Type * tt, Edf_Field * tf, bool unk=false);

	void		gen_setAt_simple(ofstream& out, Edf_Type * tt, Edf_Field * tf, bool unk=false);
	void		gen_existAt_simple(ofstream& out, Edf_Type * tt, Edf_Field * tf, bool unk=false);
	void		gen_clearAt_simple(ofstream& out, Edf_Type * tt, Edf_Field * tf, bool unk=false);
	void		gen_getAt_simple(ofstream& out, Edf_Type * tt, Edf_Field * tf, bool unk=false);
	void		gen_encAt_simple(ofstream& out, Edf_Type * tt, Edf_Field * tf, bool unk=false);
	void		gen_sizeAt_simple(ofstream& out, Edf_Type * tt, Edf_Field * tf, bool unk=false);
	void 		gen_count_simple(ofstream& out, Edf_Type * tt, Edf_Field * tf, bool unk=false);
	void 		gen_decAt_simple(ofstream& out, Edf_Type * tt, Edf_Field * tf, bool unk=false);
	void		gen_methodAt_simple(ofstream& out, Edf_Type * tt, Edf_Field * tf, bool unk=false);
	
	/* Seq B/C */
	void		gen_set_seq(ofstream& out, Edf_Type * tt, Edf_Field * tf, bool unk=false);
	void		gen_exist_seq(ofstream& out, Edf_Type * tt, Edf_Field * tf, bool unk=false);
	void		gen_clear_seq(ofstream& out, Edf_Type * tt, Edf_Field * tf, bool unk=false);
	void		gen_get_seq(ofstream& out, Edf_Type * tt, Edf_Field * tf, bool unk=false);
	void		gen_enc_seq(ofstream& out, Edf_Type * tt, Edf_Field * tf, bool unk=false);
	void 		gen_size_seq(ofstream& out, Edf_Type * tt, Edf_Field * tf, bool unk=false);
	void 		gen_dec_seq(ofstream& out, Edf_Type * tt, Edf_Field * tf, bool unk=false);
	void		gen_method_string(ofstream& out, Edf_Type * tt, Edf_Field * tf, bool unk=false);
		
	void		gen_setAt_seq(ofstream& out, Edf_Type * tt, Edf_Field * tf, bool unk=false);
	void		gen_existAt_seq(ofstream& out, Edf_Type * tt, Edf_Field * tf, bool unk=false);
	void		gen_clearAt_seq(ofstream& out, Edf_Type * tt, Edf_Field * tf, bool unk=false);
	void		gen_getAt_seq(ofstream& out, Edf_Type * tt, Edf_Field * tf, bool unk=false);
	void		gen_encAt_seq(ofstream& out, Edf_Type * tt, Edf_Field * tf, bool unk=false);
	void 		gen_sizeAt_seq(ofstream& out, Edf_Type * tt, Edf_Field * tf, bool unk=false);
	void 		gen_count_seq(ofstream& out, Edf_Type * tt, Edf_Field * tf, bool unk=false);
	void 		gen_decAt_seq(ofstream& out, Edf_Type * tt, Edf_Field * tf, bool unk=false);
	void		gen_methodAt_string(ofstream& out, Edf_Type * tt, Edf_Field * tf, bool unk=false);

	/* Pack */
	void 		gen_pack_clean(ofstream& out, Edf_Pack* tp, bool is_ref=false, bool is_pointer=false);
	void 		gen_pack_ftbl(ofstream& out, Edf_Pack* tp, bool is_ref=false, bool is_pointer=false);
	void 		gen_pack_init(ofstream& out, Edf_Pack* tp, bool is_ref=false, bool is_pointer=false);

	void		gen_set_Pack(ofstream& out, Edf_Type * tt, Edf_Field * tf, bool unk=false);
	void		gen_exist_Pack(ofstream& out, Edf_Type * tt, Edf_Field * tf, bool unk=false);
	void		gen_clear_Pack(ofstream& out, Edf_Type * tt, Edf_Field * tf, bool unk=false);
	void		gen_get_Pack(ofstream& out, Edf_Type * tt, Edf_Field * tf, bool unk=false);
	void		gen_enc_Pack(ofstream& out, Edf_Type * tt, Edf_Field * tf, bool unk=false);
	void		gen_size_Pack(ofstream& out, Edf_Type * tt, Edf_Field * tf, bool unk=false);
	void		gen_dec_Pack(ofstream& out, Edf_Type * tt, Edf_Field * tf, bool unk=false);
	void		gen_method_Pack(ofstream& out, Edf_Type * tt, Edf_Field * tf, bool unk=false);

	void		gen_setAt_Pack(ofstream& out, Edf_Type * tt, Edf_Field * tf, bool unk=false);
	void		gen_existAt_Pack(ofstream& out, Edf_Type * tt, Edf_Field * tf, bool unk=false);
	void		gen_clearAt_Pack(ofstream& out, Edf_Type * tt, Edf_Field * tf, bool unk=false);
	void		gen_getAt_Pack(ofstream& out, Edf_Type * tt, Edf_Field * tf, bool unk=false);
	void		gen_encAt_Pack(ofstream& out, Edf_Type * tt, Edf_Field * tf, bool unk=false);
	void		gen_sizeAt_Pack(ofstream& out, Edf_Type * tt, Edf_Field * tf, bool unk=false);
	void		gen_count_Pack(ofstream& out, Edf_Type * tt, Edf_Field * tf, bool unk=false);
	void		gen_decAt_Pack(ofstream& out, Edf_Type * tt, Edf_Field * tf, bool unk=false);
	void		gen_methodAt_Pack(ofstream& out, Edf_Type * tt, Edf_Field * tf, bool unk=false);
	
	void 		gen_pack_encode(ofstream& out, Edf_Pack* tp, bool is_ref=false, bool is_pointer=false);
	void 		gen_pack_decode(ofstream& out, Edf_Pack* tp, bool is_ref=false, bool is_pointer=false);
	void 		gen_pack_sizeof(ofstream& out, Edf_Pack* tp, bool is_ref=false, bool is_pointer=false);
	
public:
	string 						_vg_node;	
	std::vector<Edf_Field *> 	_vg_clue;
private:
	bool 						_use_inc_prefix;
	string 						_ns_open;
	string 						_ns_close;
	std::ofstream 				_f_types;
	std::ofstream 				_f_types_impl;
	std::ofstream 				_f_header;
};

#define EDF_FID_FACTOR  	32

bool 		Edf_GenerC::is_complex(Edf_Type* tt){
   tt = get_true_type(tt);
   return	(tt->is_struct() || tt->is_union() || tt->is_pack() ||	
   			(
   				tt->is_basic() && 
   				(((Edf_Basic*)tt)->get_basic()  == TYPE_STRING)
   			)
   			);
}

string 		Edf_GenerC::get_typeenum(Edf_Type* tt) {
	string type = "EDF_T_VOID;";
	if (tt->is_basic()) {
		switch (((Edf_Basic*)tt)->get_basic()) {
			case TYPE_VOID   : type = "EDF_T_VOID"		; break;
			case TYPE_S8	 : type = "EDF_T_S8"   		; break;
			case TYPE_S16    : type = "EDF_T_S16"    	; break;
			case TYPE_S32    : type = "EDF_T_S32"    	; break;
			case TYPE_S64    : type = "EDF_T_S64"    	; break;
			case TYPE_U8	 : type = "EDF_T_U8"   		; break;
			case TYPE_U16    : type = "EDF_T_U16"    	; break;
			case TYPE_U32    : type = "EDF_T_U32"    	; break;
			case TYPE_U64    : type = "EDF_T_U64"    	; break;
			case TYPE_STRING : type = "EDF_T_STRING" 	; break;
			case TYPE_DOUBLE : type = "EDF_T_DOUBLE" 	; break;
			default: type = "EDF_T_VOID";
		}
	}
	else if (tt->is_enum()) {
		type = "EDF_T_ENUM";
	}
	else if (tt->is_union()) {
		type = "EDF_T_UNION";
	}
	else if (tt->is_struct()) {
		type = "EDF_T_STRUCT";
	}
	else if (tt->is_pack()) {
		type = "EDF_T_PACK";
	}

	return type;
}

string 	Edf_GenerC::get_typename_basic(ET_Baisc tb) {
	switch (tb) {
		case TYPE_VOID:			return "void    ";			break;
		case TYPE_S8:			return "_sInt8  ";			break;
		case TYPE_S16:			return "_sInt16 ";			break;
		case TYPE_S32:			return "_sInt32 ";			break;
		case TYPE_S64:			return "_sInt64 ";			break;
		case TYPE_U8:			return "_uInt8  ";			break;
		case TYPE_U16:			return "_uInt16 ";			break;
		case TYPE_U32:			return "_uInt32 ";			break;
		case TYPE_U64:			return "_uInt64 ";			break;
		case TYPE_DOUBLE:		return "_dFloat ";			break;
		case TYPE_STRING:		return "_cSeq   ";			break;
		default:
			throw "basisc type :" + Edf_Basic::basic_name(tb);
			break;
	}
}

	
_sInt32 	Edf_GenerC::get_simple_len(Edf_Type * tt) {
	if(tt->is_basic()){
		switch (((Edf_Basic*)tt)->get_basic()) {
			case TYPE_S8:			return (_sInt32)sizeof(_sInt8);			break;
			case TYPE_S16:			return (_sInt32)sizeof(_sInt16);		break;
			case TYPE_S32:			return (_sInt32)sizeof(_sInt32);		break;
			case TYPE_S64:			return (_sInt32)sizeof(_sInt64);		break;
			case TYPE_U8:			return (_sInt32)sizeof(_uInt8);			break;
			case TYPE_U16:			return (_sInt32)sizeof(_uInt16);		break;
			case TYPE_U32:			return (_sInt32)sizeof(_uInt32);		break;
			case TYPE_U64:			return (_sInt32)sizeof(_uInt64);		break;
			case TYPE_DOUBLE:		return (_sInt32)sizeof(_dFloat);		break;
			default:
				throw "basic type :" + Edf_Basic::basic_name(((Edf_Basic*)tt)->get_basic());
				break;
		}
	}
	else if(tt->is_enum()){
		return (_sInt32)sizeof(_sInt32);
	}
	else{
		throw "Simple Type :" + tt->get_name();
	}
	return 0;
}

int 		Edf_GenerC::get_type_id(Edf_Type* tt) {
	if (tt->is_basic()) {
		switch (((Edf_Basic*)tt)->get_basic()) {
			case TYPE_VOID   : return (int)EDF_T_VOID		; break;
			case TYPE_S8	 : return (int)EDF_T_S8   		; break;
			case TYPE_S16    : return (int)EDF_T_S16		; break;
			case TYPE_S32    : return (int)EDF_T_S32    	; break;
			case TYPE_S64    : return (int)EDF_T_S64    	; break;
			case TYPE_U8	 : return (int)EDF_T_U8   		; break;
			case TYPE_U16    : return (int)EDF_T_U16    	; break;
			case TYPE_U32    : return (int)EDF_T_U32    	; break;
			case TYPE_U64    : return (int)EDF_T_U64    	; break;
			case TYPE_DOUBLE : return (int)EDF_T_DOUBLE		; break;
			case TYPE_STRING : return (int)EDF_T_STRING		; break;
			default: 
				return (int)EDF_T_VOID;
				break;
		}
	}

	else if (tt->is_enum()) {
		return (int)EDF_T_S32;
	}
	else if (tt->is_union()) {
		return (int)EDF_T_UNION;
	}
	else if (tt->is_struct()) {
		return (int)EDF_T_STRUCT;
	}
	else if (tt->is_pack()) {
		return (int)EDF_T_PACK;
	}

	return 0;
}

string 	Edf_GenerC::get_typename(Edf_Type* tt, bool is_typedef, bool arg) {
	if (tt->is_basic()) {
		string bname = get_typename_basic(((Edf_Basic*)tt)->get_basic());
		if (!arg) {
			return bname;
		}
		else {
			return "const " + bname;
		}
	}

	string prefix="";
	if (is_typedef && (tt->is_struct())) {
		prefix = "struct ";
	}
	if (is_typedef && (tt->is_union())) {
		prefix = "union ";
	}
	if (is_typedef && (tt->is_enum())) {
		prefix = "enum ";
	}
	if (is_typedef && (tt->is_pack())) {
		prefix = "struct ";
	}

	string pname="";
	pname = prefix + tt->get_name();
	if (!arg) {
		return pname;
	}
	else {
		return "const " + pname;
	}
	
}

string 	Edf_GenerC::get_inc_prefix(const Edf_Prog& prog) {
	string inc_prefix = prog.get_prefix();
	if (!_use_inc_prefix|| (inc_prefix.size() > 0 && inc_prefix[0] == '/')) {
		return "";
	}

	string::size_type last = string::npos;
	if ((last = inc_prefix.rfind("/")) != string::npos) {
		return inc_prefix.substr(0, last) + "/" + _output + "/";
	}
	return "";
}

#if  _OS_LINUX
	#include <sys/types.h>
	#include <sys/stat.h>
	#define MK_DIR(nDir) 	mkdir(nDir, S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH)
#else
	#include <direct.h>
	#define MK_DIR(nDir) 	_mkdir(nDir)
#endif

void 	Edf_GenerC::generator_init() {
	MK_DIR(get_output().c_str());

	_dec_unknown 	= "_xDecUnk_" + _name;
	string s_types 	= get_output() + _name+"_types.h";
	_f_types.open(s_types.c_str());
	
	string s_types_impl = get_output()+_name+"_types.c";
	_f_types_impl.open(s_types_impl.c_str());

	/* Header File */
  	_f_types 		<<    comment();
  	_f_types 		<<
    "#ifndef " 		<< "_INC_" << _name << "_H" << endl <<
    "#define " 		<< "_INC_" << _name << "_H" << endl <<  endl;

	_f_types 		<< "#include \"edf_types.h\"" << endl <<	endl;

	/* Including */
	const vector<Edf_Prog*>& includes = 	_prog->get_includes();
	for (size_t i = 0; i < includes.size(); ++i) {
		_f_types <<
	 	"#include \"" << get_inc_prefix(*(includes[i])) <<
	 	includes[i]->get_name() << "_types.h\"" << endl;
	}
	_f_types << endl;

	/* Namespace */
	#if 0
	_f_types <<	_ns_open << endl;
	#endif

	/* Source File */
 	_f_types_impl   <<	comment();
	_f_types_impl 	<<	"#include \"" 	<< 	get_inc_prefix(*get_prog()) << _name << 	"_types.h\"" << endl;

	/* Namespace */
	_f_types_impl << _ns_open << endl;

	/* Gen Unknown */
	gen_unknown(_f_types_impl, _name);
}


void 	Edf_GenerC::generator_fini() {
	/* Namespace */
	_f_types 		<<	_ns_close 	<< endl;
	_f_types 		<<	"#endif" 	<< endl;
	_f_types_impl 	<<	_ns_close 	<< endl;
	_f_types.close();
	_f_types_impl.close();
}


void 	Edf_GenerC::gen_typedef(Edf_Typedef* tt) {
	_f_types <<
	indent() << "typedef " << get_typename(tt->get_type(), true) << " " << tt->get_symbol() << ";" << endl <<
	endl;
}

void 	Edf_GenerC::gen_enum(Edf_Enum* te) {
	// Add the Comment 
	string sComment = "Prototype Definition of Enum: ";
	sComment += te->get_name();
	gen_comment(_f_types, sComment);

	_f_types <<
	indent() << "typedef enum " << te->get_name() << " {" << endl;
	indent_up();

	vector<Edf_EnumVal*> constants = te->get_constants();
	vector<Edf_EnumVal*>::iterator iter;

	bool first = true;
	for (iter = constants.begin(); iter != constants.end(); ++iter) {
		if (first) {
			first = false;
		} else {
			_f_types << "," 	<< endl;
		}
		_f_types 	<< 	indent()<< (*iter)->get_name();
		if ((*iter)->has_value()) {
			_f_types<<	" = " 	<< (*iter)->get_value();
		}
	}
	
	indent_down();
	_f_types 	<<	endl <<
	"}" 		<< te->get_name()	<< ";" 
	
	<<  endl 
	<<	endl;
}


void 	Edf_GenerC::gen_consts(std::vector<Edf_Const*> consts) {
	vector<Edf_Const*>::iterator iter;

	indent_up();
	for (iter = consts.begin(); iter != consts.end(); ++iter) {
		const_value_print(	_f_types, 				(*iter)->get_name(), 
							(*iter)->get_type(), 	(*iter)->get_value());
	}
	indent_down();
}


void 	Edf_GenerC::const_value_print(ofstream& out, string name, Edf_Type* type, Edf_ConstVal* value) {
	type = get_true_type(type);
	if (type->is_basic()) {
		string v2 = const_value_render(out, name, type, value);
		indent(out) << name << " = " << v2 << ";" << endl << endl;
	} 
	else if (type->is_enum()) {
		indent(out) << name << " = (" << get_typename(type) << ")" << value->get_sInt32() << ";" << endl <<	endl;
	} 
	else {
		throw "INVALID TYPE: " + type->get_name();
	}
}

string 	Edf_GenerC::const_value_render(ofstream& out, string name, Edf_Type* type, Edf_ConstVal* value) {
	std::ostringstream render;
	if (type->is_basic()) {
		ET_Baisc tb = ((Edf_Basic*)type)->get_basic();
		switch (tb) {
			case TYPE_S8:		render << value->get_sInt8();		break;
			case TYPE_S16:		render << value->get_sInt16();		break;
			case TYPE_S32:		render << value->get_sInt32();		break;
			case TYPE_S64:		render << value->get_sInt64();		break;
			case TYPE_U8:		render << value->get_uInt8();		break;
			case TYPE_U16:		render << value->get_uInt16();		break;
			case TYPE_U32:		render << value->get_uInt32();		break;
			case TYPE_U64:		render << value->get_uInt64();		break;
			case TYPE_DOUBLE:	render << value->get_double();		break;
			case TYPE_STRING:	render << "\"" + value->get_string() + "\"";		break;
			default:
				throw "basic type " + Edf_Basic::basic_name(tb);
				break;
		}
	}
	
 	else if (type->is_enum()) {
		render << "(" << get_typename(type) << ")" << value->get_sInt32();
	} 

	else {
		string t = tmp("tmp");
		indent(out) << get_typename(type) << " " << t << ";" << endl;
		const_value_print(out, t, type, value);
		render << t;
	}

	return render.str();
}


/* -----------------------------------------------------------------------------------
** 	Global Marco.
*/
#define 	IF_VAR_SIMPLE() \
	do{\
		indent(out) << "_xGet_" 	<< ttn	 <<"_" << fn << "," << endl;\
		indent(out) << "_xSet_" 	<< ttn	 <<"_" << fn << "," << endl;\
		indent(out) << "_xExist_"	<< ttn	 <<"_" << fn << "," << endl;\
		indent(out) << "_xClear_"	<< ttn	 <<"_" << fn << endl;\
	}while(0)
	
#define 	IF_VAR_SIMPLE_AT() \
	do{\
		indent(out) << "_xGetAt_"	<< ttn	 <<"_" << fn << "," << endl;\
		indent(out) << "_xSetAt_"	<< ttn	 <<"_" << fn << "," << endl;\
		indent(out) << "_xExistAt_" << ttn	 <<"_" << fn << "," << endl;\
		indent(out) << "_xClearAt_" << ttn	 <<"_" << fn << "," << endl;\
		indent(out) << "_xCount_" 	<< ttn	 <<"_" << fn << endl;\
	}while(0)
	
#define 	IF_VAR_COMPLEX() \
	do{\
		indent(out) << "_xGet_" 	<< ttn	 <<"_" << fn << "," << endl;\
		indent(out) << "_xSet_" 	<< ttn	 <<"_" << fn << "," << endl;\
		indent(out) << "_xExist_"	<< ttn	 <<"_" << fn << "," << endl;\
		indent(out) << "_xClear_"	<< ttn	 <<"_" << fn << endl;\
	}while(0)
		
#define 	IF_VAR_COMPLEX_AT() \
	do{\
		indent(out) << "_xGetAt_"	<< ttn	 <<"_" << fn << "," << endl;\
		indent(out) << "_xSetAt_"	<< ttn	 <<"_" << fn << "," << endl;\
		indent(out) << "_xExistAt_" << ttn	 <<"_" << fn << "," << endl;\
		indent(out) << "_xClearAt_" << ttn	 <<"_" << fn << "," << endl;\
		indent(out) << "_xCount_" << ttn	 <<"_" << fn << endl;\
	}while(0)
	
#define 	IF_VAR_STRUCT() \
	do{\
		indent(out) << "_xGet_" 	<< ttn	 <<"_" << fn << "," << endl;\
		indent(out) << "_xSet_" 	<< ttn	 <<"_" << fn << "," << endl;\
		indent(out) << "_xExist_"	<< ttn	 <<"_" << fn << "," << endl;\
		indent(out) << "_xClear_"	<< ttn	 <<"_" << fn << "," << endl;\
		indent(out) << "_xInit_"	<< ttn	 <<"_" << fn << endl;\
	}while(0)
		
#define 	IF_VAR_STRUCT_AT() \
	do{\
		indent(out) << "_xGetAt_"	<< ttn	 <<"_" << fn << "," << endl;\
		indent(out) << "_xSetAt_"	<< ttn	 <<"_" << fn << "," << endl;\
		indent(out) << "_xExistAt_" << ttn	 <<"_" << fn << "," << endl;\
		indent(out) << "_xClearAt_" << ttn	 <<"_" << fn << "," << endl;\
		indent(out) << "_xCount_" 	<< ttn	 <<"_" << fn << "," << endl;\
		indent(out) << "_xInit_"	<< ttn	 <<"_" << fn << endl;\
	}while(0)

/* 
** In Current Design, We Use the Getter/Setter.
** Integer
*/
#define 	IF_DECL_SIMPLE() \
	do{\
		indent(out) << tfn <<  " (*get  )(" << ttn	 << " *thiz);" << endl;\
		indent(out) << "_sInt32 (*set  )("	<< ttn	 << " *thiz, " << tfn << " val);" << endl;\
		indent(out) << "_sInt32 (*exist)("	<< ttn	 << " *thiz);" << endl;\
		indent(out) << "void    (*clear)("	<< ttn	 << " *thiz);" << endl;\
	}while(0)
	
#define 	IF_DECL_SIMPLE_AT() \
	do{\
		indent(out) << tfn << " (*getAt  )(" << ttn	 << " *thiz, _uInt32 idx);" 	<< endl;\
		indent(out) << "_sInt32 (*setAt  )(" << ttn  << " *thiz, _uInt32 idx, " << tfn << " val);" << endl;\
		indent(out) << "_sInt32 (*existAt)(" << ttn  << " *thiz, _uInt32 idx);" << endl;\
		indent(out) << "void    (*clearAt)(" << ttn  << " *thiz, _uInt32 idx);" << endl;\
		indent(out) << "_sInt32 (*count  )(" << ttn  << " *thiz);" 	<< endl;\
	}while(0)
	
#define 	IF_DECL_COMPLEX() \
	do{\
		indent(out) << tfn <<	"*(*get )(" << ttn	 << " *thiz);" << endl;\
		indent(out) << "_sInt32 (*set  )("	<< ttn	 << " *thiz);" << endl;\
		indent(out) << "_sInt32 (*exist)("	<< ttn	 << " *thiz);" << endl;\
		indent(out) << "void    (*clear)("	<< ttn	 << " *thiz);" << endl;\
	}while(0)
		
#define 	IF_DECL_COMPLEX_AT() \
	do{\
		indent(out) << tfn << "*(*getAt )(" 	<< ttn	<< " *thiz, _uInt32 idx);" << endl;\
		indent(out) << "_sInt32 (*setAt  )(" 	<< ttn  << " *thiz, _uInt32 idx);" << endl;\
		indent(out) << "_sInt32 (*existAt)(" 	<< ttn  << " *thiz, _uInt32 idx);" << endl;\
		indent(out) << "void    (*clearAt)("	<< ttn	<< " *thiz, _uInt32 idx);" << endl;\
		indent(out) << "_sInt32 (*count  )(" 	<< ttn  << " *thiz);" << endl;\
	}while(0)
	
#define 	IF_DECL_STRUCT() \
	do{\
		string sComment = "Interal Method: ";\
		gen_comment(out, sComment);\
		indent(out) << tfn << "*(*get )(" 	<< ttn	 << " *thiz);" << endl;\
		indent(out) << "_sInt32 (*set  )("	<< ttn	 << " *thiz);" << endl;\
		indent(out) << "_sInt32 (*exist)("	<< ttn	 << " *thiz);" << endl;\
		indent(out) << "void    (*clear)("	<< ttn	 << " *thiz);" << endl;\
		indent(out) << "void	(*init )(void  *thiz);" << endl;\
	}while(0)
		
#define 	IF_DECL_STRUCT_AT() \
	do{\
		string sComment = "Interal Method: ";\
		gen_comment(out, sComment);\
		indent(out) << tfn << "*(*getAt )("  << ttn	<< " *thiz, _uInt32 idx);" << endl;\
		indent(out) << "_sInt32 (*setAt  )(" << ttn << " *thiz, _uInt32 idx);" << endl;\
		indent(out) << "_sInt32 (*existAt)(" << ttn << " *thiz, _uInt32 idx);" << endl;\
		indent(out) << "void    (*clearAt)(" << ttn << " *thiz, _uInt32 idx);" << endl;\
		indent(out) << "_sInt32 (*count  )(" << ttn << " *thiz);" << endl;\
		indent(out) << "void	(*init	 )(void  *thiz);" << endl;\
	}while(0)


/* 
** In Current Design, We Use the Getter/Setter.
** Integer
*/
/* -----------------------------------------------------------------------------------
**	Clue OFF.
*/

#define 	CLUE_OFF_V() \
do{\
	indent(out) << "_uInt32 _vOff = " \
				<< "(_uInt32)(_uInt64)(&(((" << _vg_node << "*)0)->" << clue << "." <<fn << ")) - "\
				<< "(_uInt32)(_uInt64)(&(((" << _vg_node << "*)0)->" << clue << "));"\
				<< endl;\
}while(0)

#define 	CLUE_OFF_S() \
do{\
	indent(out) << "_uInt32 _sOff = " \
				<< "(_uInt32)sizeof(((" << _vg_node << "*)0)->" << clue << "." <<fn << ");"\
				<< endl;\
}while(0)
	
#define 	CLUE_OFF_P() \
do{\
	indent(out) << "_uInt32 _pOff = " \
				<< "(_uInt32)(_uInt64)(&(((" << _vg_node << "*)0)->" << clue << "._presence._fid[" << nFid << "]" << ")) - "\
				<< "(_uInt32)(_uInt64)(&(((" << _vg_node << "*)0)->" << clue << "));"\
				<< endl;\
}while(0)

#define 	CLUE_OFF_A() \
do{\
	indent(out) << "_uInt32 _pOff = " \
				<< "(_uInt32)(_uInt64)(((" << _vg_node << "*)0)->" << clue << "._presence." << fn << ") - "\
				<< "(_uInt32)(_uInt64)(&(((" << _vg_node << "*)0)->" << clue << "));"\
				<< endl;\
}while(0)

#define 	CLUE_OFF_IDX_V() \
do{ \
	indent(out) << "_uInt32 _vOff = " \
				<< "(_uInt32)(_uInt64)(&(((" << _vg_node << "*)0)->" << clue << "." << fn << "[_idx])) - "\
				<< "(_uInt32)(_uInt64)(&(((" << _vg_node << "*)0)->" << clue << "));" \
				<< endl;\
}while(0)

#define 	CLUE_OFF_IDX_VBEG() \
do{ \
	indent(out) << "_uInt32 _vOff = " \
				<< "(_uInt32)(_uInt64)(((" << _vg_node << "*)0)->" << clue << "." << fn << ") - "\
				<< "(_uInt32)(_uInt64)(&(((" << _vg_node << "*)0)->" << clue << "));" \
				<< endl;\
}while(0)

#define 	CLUE_OFF_IDX_S() \
do{ \
	indent(out) << "_uInt32 _sOff = " \
				<< "(_uInt32)sizeof(((" << _vg_node << "*)0)->" << clue << "." << fn << "[0]);"\
				<< endl;\
}while(0)

#define 	CLUE_OFF_IDX_P() \
do{ \
	indent(out) << "_uInt32 _pOff = " \
				<< "(_uInt32)(_uInt64)(&(((" << _vg_node << "*)0)->" << clue << "._presence._fid[" + nFid + "]" << ")) - "\
				<< "(_uInt32)(_uInt64)(&(((" << _vg_node << "*)0)->" << clue << "));" \
				<< endl;\
}while(0)

#define 	CLUE_OFF_IDX_PBEG() \
do{\
	indent(out) << "_uInt32 _pOff = " \
				<< "(_uInt32)(_uInt64)(((" << _vg_node << "*)0)->" << clue << "._presence._fid) - "\
				<< "(_uInt32)(_uInt64)(&(((" << _vg_node << "*)0)->" << clue << "));"\
				<< endl;\
}while(0)


#define 	CLUE_OFF_IDX_A() \
do{ \
	indent(out) << "_uInt32 _aOff = " \
				<< "(_uInt32)(_uInt64)(&(((" << _vg_node << "*)0)->" << clue << "._presence." << fn << "[_idx/" << EDF_FID_FACTOR << "]"<< ")) - "\
				<< "(_uInt32)(_uInt64)(&(((" << _vg_node << "*)0)->" << clue << "));" \
				<< endl;\
}while(0)

#define 	CLUE_OFF_IDX_ABEG() \
do{ \
	indent(out) << "_uInt32 _aOff = " \
				<< "(_uInt32)(_uInt64)(((" << _vg_node << "*)0)->" << clue << "._presence." << fn << ") - "\
				<< "(_uInt32)(_uInt64)(&(((" << _vg_node << "*)0)->" << clue << "));" \
				<< endl;\
}while(0)

#define 	CLUE_OFF_IDX_APARAM() \
do{ \
	indent(out) << "_aOff = " \
				<< "(_uInt32)(_uInt64)(((" << _vg_node << "*)0)->" << clue << "._presence." << fn << ") - "\
				<< "(_uInt32)(_uInt64)(&(((" << _vg_node << "*)0)->" << clue << "));" \
				<< endl;\
}while(0)


/* -----------------------------------------------------------------------------------
** 	Element Var & Ptr.
*/
#define 	ELEM_V() 	"(*(" 	<< tfn << "*)((_uInt8 *)_xPtr + _vOff))"
#define 	ELEM_VPTR() 	"((" 	<< tfn << "*)((_uInt8 *)_xPtr + _vOff))"

#define 	ELEM_P() 	"(*(_uInt32 *)((_uInt8 *)_xPtr + _pOff))"
#define 	ELEM_PPTR() 	"((_uInt32 *)((_uInt8 *)_xPtr + _pOff))"

#define 	ELEM_A() 	"(*(_uInt32 *)((_uInt8 *)_xPtr + _aOff))"
#define 	ELEM_APTR() 	"((_uInt32 *)((_uInt8 *)_xPtr + _aOff))"

#define 	CLUE_PREFIX() \
	std::vector<Edf_Field *>::const_iterator f_iter;\
	string f_name, clue  = "";\
	string prefix= _vg_node;\
	f_iter = _vg_clue.begin();\
	f_name = (*f_iter)->get_name();\
	prefix += "_" + f_name;		clue   += f_name;\
	if((*f_iter)->get_repeat() > 1){\
		clue   += "[0]";\
	}\
	++f_iter;\
	while(f_iter != _vg_clue.end()) {\
		f_name = (*f_iter)->get_name();\
		prefix += "_" + f_name;	clue   += "." + f_name;\
		if((*f_iter)->get_repeat() > 1){\
			clue   += "[0]";\
		}\
		++f_iter;\
	}\


void	Edf_GenerC::gen_comment(ofstream& out, string& sComment)
{
	indent(out) << "/*	" << sComment << " */" << endl;
}
		

void	Edf_GenerC::gen_unknown(ofstream& out, string& sModule)
{
	// Add the Comment 
	string sComment = "Module Definition of Dec Unknown: ";
	sComment += sModule;
	gen_comment(out, sComment);
	
	indent(out) << "static _sInt32 _xDecUnk_" << sModule << "(_uInt8 * _oPtr, _sInt32 _oLen){" << endl;
	indent_up();
		indent(out) << "return 	_xDec_Unknown(_oPtr, _oLen);" << endl;
	indent_down();
	indent(out) << "}" << endl << endl;
}



#define 	_ENDVAL_HTONL(val) 	\
	do{\
		indent(out) << "#ifdef __USE_EDF_ENDIAN" << endl;\
		indent_up();\
		indent(out) << val << " = htonl(" << val << ");" << endl;\
		indent_down();\
		indent(out) << "#endif" << endl;\
	}while(0)
	
#define 	_ENDVAL_NTOHL(val) 	\
	do{\
		indent(out) << "#ifdef __USE_EDF_ENDIAN" << endl;\
		indent_up();\
		indent(out) << val << " = ntohl(" << val << ");" << endl;\
		indent_down();\
		indent(out) << "#endif" << endl;\
	}while(0)

/* -------------------------------------------------------------------------------------------
** Generate the Field.
*/
string	Edf_GenerC::gen_field_init(Edf_Field* tf){
	string result = "";
	Edf_Type* type = get_true_type(tf->get_type());
	
	if (type->is_basic()) {
		ET_Baisc tb = ((Edf_Basic*)type)->get_basic();
		switch (tb) {
			case TYPE_VOID:
				break;
			case TYPE_S8:
			case TYPE_S16:
			case TYPE_S32:
			case TYPE_S64:
			case TYPE_U8:
			case TYPE_U16:
			case TYPE_U32:
			case TYPE_U64:
				result += " = 0";
				break;
			case TYPE_DOUBLE:
				result += " = (double)0.0";
				break;
			case TYPE_STRING:
				result += " = \"\"";
				break;
			default:
				throw "no C initializer for base type " + Edf_Basic::basic_name(tb);
				break;
		}
	} 
	else if (type->is_enum()) {
		result += " = (" + get_typename(type) + ")0";
	}
	
	return result;
}

void	Edf_GenerC::gen_field_decl(ofstream& out, Edf_Field* tf, bool pointer, bool constant, int repeat) {
	/* Const Type * Name[xxx] */
	string		cn, tn;
	Edf_Type *	tt;

	cn = "";
	if (constant) {
		cn = "const ";
	}
	tt = tf->get_type();	tn = get_typename(tt);
	if(tn != ""){
		indent(out) << cn << tn;
		if (pointer) {
			out << "*";
		}
	}
	else if(tt->is_struct()){
		/* Annonymous ? */
		gen_pack_def(out, (Edf_Struct*)tt, true);
	}
	else if(tt->is_union()){
		gen_pack_def(out, (Edf_Union*)tt, true);
	}
	out << " " << tf->get_name();
	
	if(repeat > 1){
		out << "[" << repeat << "]";
	}
	out << ";" << endl;
	return;
}

void 	Edf_GenerC::gen_field_def_if(ofstream& out, Edf_Type * tt, Edf_Field* tf, bool pointer) {
	string 	tfn, ttn;
	Edf_Type* type = get_true_type(tf->get_type());
	
	ttn = get_typename(tt);
	if(ttn == ""){
		ttn = "void ";
	}
	
	tfn = get_typename(type);
	if(tfn != ""){
		indent(out) << "struct" << " {" << endl;
		indent_up();
		
		if(tf->get_repeat() > 1){
			if(is_complex(type)){
				IF_DECL_COMPLEX_AT();
			}else{
				IF_DECL_SIMPLE_AT();
			}
		}
		else{
			if(is_complex(type)){
				IF_DECL_COMPLEX();
			}else{
				IF_DECL_SIMPLE();
			}
		}
		
		indent_down();
		indent(out) << "}" << tf->get_name() << ";" << endl;
	}
	else if(type->is_struct()){
		gen_pack_def_if(out, (Edf_Struct *)type, true);
		tfn = "void    ";
		if(tf->get_repeat() > 1){
			IF_DECL_STRUCT_AT();
		}else{
			IF_DECL_STRUCT();
		}
		indent_down();
		indent(out) << "}" << tf->get_name() << ";" << endl;
	}
	else if(type->is_union()){
		gen_pack_def_if(out, (Edf_Union*)type, true);
		tfn = "void    ";
		if(tf->get_repeat() > 1){
			IF_DECL_STRUCT_AT();
		}else{
			IF_DECL_STRUCT();
		}
		indent_down();
		indent(out) << "}" << tf->get_name() << ";" << endl;
	}
	
	return;
}

void 	Edf_GenerC::gen_field_impl_if(ofstream& out, Edf_Type * tt, Edf_Field* tf) {
	string 		tfn, ttn;
	bool 		unk;
	Edf_Type * 	ttf;
	
	ttf = get_true_type(tf->get_type());
	tfn = get_typename(ttf);	ttn = get_typename(tt);
	unk = false;
	if(ttn == ""){
		unk = true;
	}
	if(tfn != ""){
		if (ttf->is_struct() || ttf->is_union() || ttf->is_pack()) {
			if(tf->get_repeat() > 1){
				gen_methodAt_Pack(out, tt, tf, unk);
			}else{
				gen_method_Pack(out, tt, tf, unk);
			}
		} 
		
		else if (ttf->is_basic() || ttf->is_enum()) {
			/* GetAt & SetAt */
			if (ttf->is_basic()) {
				ET_Baisc tb = ((Edf_Basic*)ttf)->get_basic();
				switch (tb) {
					case TYPE_STRING:
						if(tf->get_repeat() > 1){
							gen_methodAt_string(out, tt, tf, unk);
						}else{
							gen_method_string(out, tt, tf, unk);
						}
						break;
					
					case TYPE_S8:
					case TYPE_S16:
					case TYPE_S32:
					case TYPE_S64:
					case TYPE_U8:
					case TYPE_U16:
					case TYPE_U32:
					case TYPE_U64:
					case TYPE_DOUBLE:
						if(tf->get_repeat() > 1){
							gen_methodAt_simple(out, tt, tf, unk);
						}else{
							gen_method_simple(out, tt, tf, unk);
						}
						break;

					default:
						throw "no C decoder for basisc type " + Edf_Basic::basic_name(tb);
						break;
				}
			}
			else if (ttf->is_enum()){
				if(tf->get_repeat() > 1){
					gen_methodAt_simple(out, tt, tf, unk);
				}else{
					gen_method_simple(out, tt, tf, unk);
				}
			}
			out << endl;
		}
	}
	else if(ttf->is_struct()){
		_vg_clue.push_back(tf);
			#if 0
			gen_pack_clean(out, (Edf_Struct *)ttf, true, false);
			#endif
			
			gen_pack_impl_if(out, (Edf_Struct *)ttf, true, false);
			gen_pack_ftbl(out, (Edf_Struct *)ttf, true, false);

			gen_pack_init(out, (Edf_Struct *)ttf, true, false);
			gen_pack_encode(out, (Edf_Struct *)ttf, true, false);
			gen_pack_decode(out, (Edf_Struct *)ttf, true, false);
			gen_pack_sizeof(out, (Edf_Struct *)ttf, true, false);
		_vg_clue.pop_back();
		
		if(tf->get_repeat() > 1){
			gen_methodAt_Pack(out, tt, tf, unk);
		}else{
			gen_method_Pack(out, tt, tf, unk);
		}
	}
	else if(ttf->is_union()){
		_vg_clue.push_back(tf);
			gen_pack_clean(out, (Edf_Union *)ttf, true, false);
			gen_pack_impl_if(out, (Edf_Union *)ttf, true, false);
			gen_pack_ftbl(out,(Edf_Union *)ttf, true, false);

			gen_pack_init(out, (Edf_Union *)ttf, true, false);
			gen_pack_encode(out, (Edf_Union *)ttf, true, false);
			gen_pack_decode(out, (Edf_Union *)ttf, true, false);
			gen_pack_sizeof(out, (Edf_Struct *)ttf, true, false);
		_vg_clue.pop_back();
		
		if(tf->get_repeat() > 1){
			gen_methodAt_Pack(out, tt, tf, unk);
		}else{
			gen_method_Pack(out, tt, tf, unk);
		}
	}
	else {
		throw "no C encoder for " + tf->get_name() +  "TYPE" + get_typename(tt);
	}
	return;
}


void 	Edf_GenerC::gen_field_presence(ofstream& out, Edf_Field* tf, int repeat) {
	/* Const Type * Name[xxx] */
	if(repeat > 1){
		indent(out) << "_uInt32 " << tf->get_name() << "[" << (repeat + EDF_FID_FACTOR - 1)/EDF_FID_FACTOR << "];" << endl;
	}
}

void	Edf_GenerC::gen_field_var_if(ofstream& out, Edf_Type * tt, Edf_Field* tf, bool pointer) {
	string	tfn, ttn, fn, prefix;
	Edf_Type* type = get_true_type(tf->get_type());

	fn  = tf->get_name();
	ttn = get_typename(tt);
	if(ttn == ""){
		CLUE_PREFIX();		ttn = prefix;
	}
	
	tfn = get_typename(type);
	if(tfn != ""){
		indent(out) << "{" << endl;
		indent_up();
		if(tf->get_repeat() > 1){
			if(is_complex(type)){
				IF_VAR_COMPLEX_AT();
			}else{
				IF_VAR_SIMPLE_AT();
			}
		}else{
			if(is_complex(type)){
				IF_VAR_COMPLEX();
			}else{
				IF_VAR_SIMPLE();
			}
		}
		indent_down();
		indent(out) << "}" << "," << endl;
	}
	
	else if(type->is_struct()){
		_vg_clue.push_back(tf);

		gen_pack_var_if(out, (Edf_Struct *)type, true);
		if(tf->get_repeat() > 1){
			IF_VAR_STRUCT_AT();
		}else{
			IF_VAR_STRUCT();
		}
		indent_down();
		indent(out) << "}" << "," << endl;

		_vg_clue.pop_back();
	}
	else if(type->is_union()){
		_vg_clue.push_back(tf);

		gen_pack_var_if(out, (Edf_Union*)type, true);
		if(tf->get_repeat() > 1){
			IF_VAR_STRUCT_AT();
		}else{
			IF_VAR_STRUCT();
		}
		indent_down();
		indent(out) << "}" << "," << endl;

		_vg_clue.pop_back();
	}
	
	return;
}

/* -------------------------------------------------------------------------------------------
** Generate the Pack (Struct/Union).
*/
void	Edf_GenerC::gen_pack_decl(ofstream& out, Edf_Pack* tp, bool is_ref, bool is_pointer) 
{
	#if 0
	indent(out) << "typedef struct " 
				<<setw(SET_DECL_WIDTH) << " " << tp->get_name() 
				<< " "
				<<setw(SET_DECL_WIDTH) << " " << tp->get_name() 
				<< ";" << endl << endl;
	#endif
	// Add the Comment 
	string sComment = "Exposure Method of Pack: ";
	sComment += tp->get_name();
	gen_comment(out, sComment);

	string ttn = get_typename(tp);
	
	out << "extern void    _xInit_" 	<< ttn	<< "(" << ttn	<< " * _xPtr);" <<	endl;
	out << "extern _sInt32 _xEncode_"	<< ttn	<< "(" << ttn	<< " * _xPtr, _uInt8 * _oPtr, _sInt32 _oLen, _sInt32 * _oStatus);" <<	endl;
	out << "extern _sInt32 _xDecode_"	<< ttn	<< "(" << ttn	<< " * _xPtr, _uInt8 * _oPtr, _sInt32 _oLen, _sInt32 * _oStatus);" <<	endl;
	out << "extern _sInt32 _xSizeOf_"	<< ttn	<< "(" << ttn	<< " * _xPtr);" <<	endl;
	out << endl;
}


void	Edf_GenerC::gen_pack_decl_if(ofstream& out, Edf_Pack* tp, bool is_ref, bool is_pointer) 
{
	if(!is_ref){
		#if 0
		indent(out) << "typedef struct " 
					<< setw(SET_DECL_WIDTH) << "_I_" <<	tp->get_name() 
					<< "  "
					<< setw(SET_DECL_WIDTH) << "_I_" << tp->get_name() 
					<< ";" 
					<< endl << endl;
		#endif
		
		// Add the Comment 
		string sComment = "Interface Init of Pack: ";
		sComment += tp->get_name();
		gen_comment(out, sComment);

		string ttn = get_typename(tp);
		indent(out) << "extern  void   "	
					<< setw(SET_DECL_WIDTH) <<"_I_" << tp->get_name() << "_Init(_I_" << ttn << "** _ppIf);"	<< endl
					<< endl << endl;
	}
}

void 	Edf_GenerC::gen_pack_impl_if(ofstream& out, Edf_Pack* tp, bool is_ref, bool is_pointer) 
{
	string 	tn;
	const vector<Edf_Field*>& 			members = tp->get_members();
	vector<Edf_Field*>::const_iterator 	m_iter;

	tn = tp->get_name();
	for (m_iter = members.begin(); m_iter != members.end(); ++m_iter) {
		gen_field_impl_if(out, (Edf_Type *)tp, *m_iter);
	}
}

void 	Edf_GenerC::gen_pack_def_if(ofstream& out, Edf_Pack* tp, bool is_ref, bool is_pointer) 
{
	string 	tn;
	
	if(!is_ref){
		// Add the Comment 
		string sComment = "Interface Definition of : ";
		sComment += tp->get_name();
		gen_comment(out, sComment);

		indent(out) << "typedef struct " << "_I_" << tp->get_name() << " {" << endl;
		indent_up();
		
		const vector<Edf_Field*>&			members = tp->get_members();
		vector<Edf_Field*>::const_iterator	m_iter;
		
		for (m_iter = members.begin(); m_iter != members.end(); ++m_iter) {
			gen_field_def_if(out, (Edf_Type *)tp, *m_iter, is_pointer);
			out << endl;
		}
		
		/* Encode & Decode */
		tn = tp->get_name();
		if(tn == "")
			tn = "void";
		
		sComment = "External Method Of: ";
		sComment += tp->get_name();
		gen_comment(out, sComment);
		
		indent(out) << "_sInt32 (*encode)(" << tn << " *thiz, _uInt8 * xPtr, _sInt32 xLen, _sInt32 * _oStatus);" << endl;
		indent(out) << "_sInt32 (*decode)(" << tn << " *thiz, _uInt8 * xPtr, _sInt32 xLen, _sInt32 * _oStatus);" << endl;
		indent(out) << "_sInt32 (*sizeOf)(" << tn << " *thiz);" << endl;
		indent(out) << "void    (*init  )(" << tn << " *thiz);" << endl;

		indent_down();
		indent(out) <<"}" << "_I_"  << tp->get_name() << ";" 
			<< endl
			<< endl;
	}
	else{
		// Add the Comment 
		string sComment = "Internal Interface: ";
		sComment += tp->get_name();
		gen_comment(out, sComment);

		indent(out) << "struct " << " {" << endl;
		indent_up();
		
		const vector<Edf_Field*>&			members = tp->get_members();
		vector<Edf_Field*>::const_iterator	m_iter;
		
		for (m_iter = members.begin(); m_iter != members.end(); ++m_iter) {
			gen_field_def_if(out, (Edf_Type *)tp, *m_iter, is_pointer);
			out << endl;
		}
	}
	
}


void	Edf_GenerC::gen_get_Pack(ofstream& out, Edf_Type * tt, Edf_Field * tf, bool unk)
{
	ostringstream nss;
	nss 	<<	tf->get_fid()/EDF_FID_FACTOR;
	string nFid = nss.str();	
	string tn, fn, tfn; 
	bool 	ifn; 

	fn		= tf->get_name();	
	ifn 	= true;
	tfn 	= get_typename(get_true_type(tf->get_type()));
	if(tfn == ""){
		tfn = "void ";
		ifn = false;
	}
	
	if(unk == false){
		string vFid, pFid;
		tn	 = tt->get_name();
		vFid = "_xPtr->" + fn;
		pFid = "_xPtr->_presence._fid[" + nFid	+  "]";

		out << "static " << tfn << "*_xGet_"	<< tn	<< "_"	<< fn	<< "(" 	<< tn	<< " * _xPtr){"	<<	endl;
		indent_up();
			indent(out) << "if((" << pFid << " & (0X01 << " << tf->get_fid()%EDF_FID_FACTOR << ")) == 0){"			<< endl;
			indent_up();
				if(ifn == true){
					indent(out) << "_xInit_" << tfn << "(&(" << vFid << "));" << endl;
				}
				else{
					indent(out) << "_xInit_" << tn	<< "_" << fn << "(&(" << vFid << "));" << endl;
				}
			indent_down();
			indent(out) << "}" << endl;
			
			indent(out) << "return &(" << vFid << ");" << endl;
	}
	else{
		CLUE_PREFIX();
		tn	 = "void ";
		out << "static " << tfn << "*_xGet_"	<< prefix<< "_"	<< fn	<< "(" 	<< tn	<< " * _xPtr){"	<<	endl;
		indent_up();
			CLUE_OFF_V(); CLUE_OFF_P();
			indent(out) << "if((" << ELEM_P() << " & (0X01 << "	<< tf->get_fid()%EDF_FID_FACTOR << ")) == 0){" 			<< endl;
			indent_up();
				if(ifn == true){
					indent(out) << "_xInit_" << tfn << "(" << ELEM_VPTR() << ");" << endl;
				}
				else{
					indent(out) << "_xInit_" << prefix  << "_" << fn << "(" << ELEM_VPTR() << ");" << endl;
				}
			indent_down();
			indent(out) << "}" << endl;
			indent(out) << "return " << ELEM_VPTR() << ";" << endl;
	}
	indent_down();
	out <<	"}"  
		<<	endl 
		<<	endl;
}

void	Edf_GenerC::gen_method_Pack(ofstream& out, Edf_Type * tt, Edf_Field * tf, bool unk)
{
	// Add the Comment 
	string sComment = "Methods of Pack Field: ";
	sComment += tf->get_name();
	gen_comment(out, sComment);

	gen_exist_Pack(out, tt, tf, unk);
	gen_clear_Pack(out, tt, tf, unk);
	
	gen_get_Pack(out, tt, tf, unk);
	gen_set_Pack(out, tt, tf, unk);
	
	gen_enc_Pack(out, tt, tf, unk);
	gen_size_Pack(out, tt, tf, unk);
	gen_dec_Pack(out, tt, tf, unk);
}

/*------------------------------------------------------------------------------------------------------------
** Generate the Method Struct AT
*/
void	Edf_GenerC::gen_existAt_Pack(ofstream& out, Edf_Type * tt, Edf_Field * tf, bool unk)
{
	ostringstream nss;
	nss 	<<	tf->get_fid()/EDF_FID_FACTOR;
	string nFid = nss.str();	
	string tn, fn, tfn; 
	fn		= tf->get_name();	
	tfn 	= get_typename(get_true_type(tf->get_type()));
	if(unk == false){
		string vFid, pFid, aFid;
		ostringstream ass;
		ass  << "_xPtr->_presence." << fn << "[_idx/" << EDF_FID_FACTOR << "]";
		aFid =	ass.str();

		tn	 = tt->get_name();
		vFid = "_xPtr->" + fn + "[_idx]";
		pFid = "_xPtr->_presence._fid[" + nFid	+  "]";

		out << "static _sInt32 _xExistAt_" << tn << "_" << fn	<< "(" 	<< tn << " * _xPtr,	_uInt32 _idx){" << endl;
		indent_up();
		indent(out) << "if((_idx < " << tf->get_repeat() << ") && "		<< endl;
		indent(out) << "  (" << pFid << " & (0X01 << "	<< tf->get_fid()%EDF_FID_FACTOR << "))){" 			<< endl;
		indent_up();
			indent(out) << "if(" << aFid <<  " & (0X01 <<"	<< "(_idx%" << EDF_FID_FACTOR << "))){"			<< endl;
	}
	else{
		CLUE_PREFIX();
		tn	 = "void ";

		out << "static _sInt32 _xExistAt_" << prefix<< "_"<<fn	<< "(" << tn << " * _xPtr,	_uInt32 _idx){" << endl;
		indent_up();
		CLUE_OFF_IDX_P();		CLUE_OFF_IDX_A();
		indent(out) << "if((_idx < " << tf->get_repeat() << ") && "		<< endl;
		indent(out) << "  (" << ELEM_P() << " & (0X01 << "	<< tf->get_fid()%EDF_FID_FACTOR << "))){" 		<< endl;
		indent_up();
			indent(out) << "if(" << ELEM_A() <<  " & (0X01 <<"	<< "(_idx%" << EDF_FID_FACTOR << "))){"		<< endl;
	}
		indent_up();
		indent(out) << "return (1);"	<< endl;
		indent_down();
		indent(out) << "}" 				<< endl;
	indent_down();
	indent(out) << "}" 					<< endl;
	indent(out) << "return (0);" 		<< endl;
	indent_down();
	out <<	"}" 	
		<<	endl 
		<<	endl;
}

void	Edf_GenerC::gen_clearAt_Pack(ofstream& out, Edf_Type * tt, Edf_Field * tf, bool unk)
{
	ostringstream nss;
	nss 	<<	tf->get_fid()/EDF_FID_FACTOR;
	string nFid = nss.str();	
	string tn, fn, tfn; 
	fn		= tf->get_name();	
	tfn 	= get_typename(get_true_type(tf->get_type()));
	if(unk == false){
		string vFid, pFid, aFid;
		ostringstream ass;
		ass  << "_xPtr->_presence." << fn << "[_idx/" << EDF_FID_FACTOR << "]";
		aFid =	ass.str();

		tn	 = tt->get_name();
		vFid = "_xPtr->" + fn + "[_idx]";
		pFid = "_xPtr->_presence._fid[" + nFid	+  "]";

		out << "static void _xClearAt_" << tn << "_" << fn	<< "(" 	<< tn << " * _xPtr,	_uInt32 _idx){" << endl;
		indent_up();
		indent(out) << "if((_idx < " << tf->get_repeat() << ") && "		<< endl;
		indent(out) << "  (" << pFid << " & (0X01 << "	<< tf->get_fid()%EDF_FID_FACTOR << "))){" 			<< endl;
		indent_up();
			indent(out) << aFid <<  " &= ~(0X01 <<"	<< "(_idx%" << EDF_FID_FACTOR << "));"			<< endl;
	}
	else{
		CLUE_PREFIX();
		tn	 = "void ";

		out << "static void _xClearAt_" << prefix<< "_"<<fn	<< "(" << tn << " * _xPtr,	_uInt32 _idx){" << endl;
		indent_up();
		CLUE_OFF_IDX_P();		CLUE_OFF_IDX_A();
		indent(out) << "if((_idx < " << tf->get_repeat() << ") && "		<< endl;
		indent(out) << "  (" << ELEM_P() << " & (0X01 << "	<< tf->get_fid()%EDF_FID_FACTOR << "))){" 		<< endl;
		indent_up();
			indent(out) << ELEM_A() <<  " &= ~(0X01 <<"	<< "(_idx%" << EDF_FID_FACTOR << "));"		<< endl;
	}
	indent_down();
	indent(out) << "}" 					<< endl;
	indent(out) << "return;" 		<< endl;
	indent_down();
	out <<	"}" 	
		<<	endl 
		<<	endl;
}

/* -------------------------------------------------------------------------------------------
** Generate the Union.
*/

void 	Edf_GenerC::gen_pack_clean(ofstream& out, Edf_Pack * tp, bool is_ref, bool is_pointer) 
{
	string 		ttn;	
	bool 		unk;

	unk = false;
	ttn = get_typename(tp);
	if(ttn == ""){
		unk = true;
	}
	
	const vector<Edf_Field*>&			members= tp->get_members();
	vector<Edf_Field*>::const_iterator	m_iter;
	
	int fidMax = 0;
	/* We Order all the Members */
	for (m_iter = members.begin(); m_iter != members.end(); ++m_iter) {
		Edf_Field * tFid;
		int 		fid;
		tFid = *m_iter;
		fid  = tFid->get_fid();
		if((fid >0) && (fid < 127)){
			if(fid > fidMax)
				fidMax = fid;
		}
		else{
			throw "Fid Error:" + tFid->get_name();
		}
	}
	fidMax += 1;
	
	/* Gen Enc Table */		
	if(unk == false){
		// Add the Comment 
		string sComment = "<Clean > Pack: ";
		sComment += ttn;
		gen_comment(out, sComment);
		
		out << "static void _xClean_"	<< ttn	<< "(" << ttn	<< " * _xPtr){" <<	endl;
		indent_up();
			indent(out) << "_uInt32 * _pFid = _xPtr->_presence._fid;" << endl;
	}
	else{
		CLUE_PREFIX();
		// Add the Comment 
		string sComment = "<Clean > Pack: ";
		sComment += prefix;
		gen_comment(out, sComment);

		out << "static void _xClean_"	<< prefix	<< "(void * _xPtr){"	<<	endl;
		indent_up();
			CLUE_OFF_IDX_PBEG();
			indent(out) << "_uInt32 * _pFid = " << ELEM_PPTR() << ";" << endl;
	}
		indent(out) << "_sInt32 _num;"	<< endl;
		indent(out) << "for(_num = 0; _num <" << ((fidMax + EDF_FID_FACTOR - 1)/EDF_FID_FACTOR) << "; _num++){" << endl;
		indent_up();
			indent(out) << "_pFid[_num] = 0;" << endl;
		indent_down();
		indent(out) << "}"		<< endl;

		indent(out) << "return;"<< endl;
		indent_down();	
		out <<	"}"	<<	endl	<<	endl;	
	
	return;
}


void 	Edf_GenerC::gen_pack_init(ofstream& out, Edf_Pack * tp, bool is_ref, bool is_pointer) 
{
	string 		ttn, fn;	
	bool 		unk;

	unk = false;
	ttn = get_typename(tp);
	if(ttn == ""){
		unk = true;
	}
	
	const vector<Edf_Field*>&			members= tp->get_members();
	vector<Edf_Field*>::const_iterator	m_iter;
	
	int fidMax = 0;
	int hasSeq = 0;
	/* We Order all the Members */
	for (m_iter = members.begin(); m_iter != members.end(); ++m_iter) {
		Edf_Field * tFid;
		int 		fid;
		tFid = *m_iter;
		fid  = tFid->get_fid();
		if((fid >0) && (fid < 127)){
			if(fid > fidMax)
				fidMax = fid;
		}
		else{
			throw "Fid Error:" + tFid->get_name();
		}
		if(tFid->is_sequence()){
			hasSeq = 1;
		}
	}

	fidMax += 1;
	/* Gen Enc Table */		
	if(unk == false){
		// Add the Comment 
		string sComment = "<Init  > Pack: ";
		sComment += ttn;
		gen_comment(out, sComment);
		
		out << "void _xInit_"	<< ttn	<< "(" << ttn	<< " * _xPtr){" <<	endl;
		indent_up();
			indent(out) << "_sInt32 _idx ;"	<< endl;
			indent(out) << "_uInt32 * _pFid = _xPtr->_presence._fid;" << endl;

			indent(out) << "_idx = " << ((fidMax + EDF_FID_FACTOR - 1)/EDF_FID_FACTOR) << ";" << endl;
			indent(out) << "do{ " << endl;
			indent_up();
				indent(out) << "*_pFid = 0;" 	<< endl;
				indent(out) << "++_pFid;   " 	<< endl;
			indent_down();
			indent(out) << "}while(--_idx);" 	<< endl;
				
			#if 0
			indent(out) << "for(_idx = 0; _idx < " << ((fidMax + EDF_FID_FACTOR - 1)/EDF_FID_FACTOR) << "; _idx++){" << endl;
				indent_up();
					indent(out) << "_pFid[_idx] = 0; "	<< endl;
				indent_down();
			indent(out) << "}"	<< endl;
			#endif
			
			for (m_iter = members.begin(); m_iter != members.end(); ++m_iter) {
				ostringstream ass; 
				string aFid;
				Edf_Field * tFid;
				Edf_Type  * ttf;
			
				tFid = *m_iter;
				ttf  = get_true_type(tFid->get_type());
				fn 	 = tFid->get_name();

				if(tFid->is_sequence()){
					// Add the Comment 
					sComment = "Init the Sub: A Element: " + fn;
					gen_comment(out, sComment);

					ass  << "_xPtr->_presence." << fn;
					aFid =	ass.str();
					indent(out) << "_pFid = " << aFid << ";" << endl;

					indent(out) << "_idx = " << ((tFid->get_repeat() + EDF_FID_FACTOR - 1)/EDF_FID_FACTOR) << ";" << endl;
					indent(out) << "do{ " << endl;
					indent_up();
						indent(out) << "*_pFid = 0;"	<< endl;
						indent(out) << "++_pFid;   "	<< endl;
					indent_down();
					indent(out) << "}while(--_idx);"	<< endl;
					#if 0					
					indent(out) << "_xEdfZero32(_pFid, " << ((tFid->get_repeat() + EDF_FID_FACTOR - 1)/EDF_FID_FACTOR) << ");" << endl;
					#endif
				}
			}
	}
	else{
		CLUE_PREFIX();
		// Add the Comment 
		string sComment = "<Init  > Pack: ";
		sComment += prefix;
		gen_comment(out, sComment);

		out << "static void _xInit_"<< prefix	<< "(void * _xPtr){"	<<	endl;
		indent_up();
			CLUE_OFF_IDX_PBEG();
			indent(out) << "_sInt32 _idx;"	<< endl;
			if(hasSeq){
				indent(out) << "_uInt32 _aOff = 0;"	<< endl;
			}
			indent(out) << "_uInt32 * _pFid = " << ELEM_PPTR() << ";" << endl;

			indent(out) << "_idx = " << ((fidMax + EDF_FID_FACTOR - 1)/EDF_FID_FACTOR) << ";" << endl;
			indent(out) << "do{ " << endl;
			indent_up();
				indent(out) << "*_pFid = 0;" 	<< endl;
				indent(out) << "++_pFid;   " 	<< endl;
			indent_down();
			indent(out) << "}while(--_idx);" 	<< endl;

			#if 0
			indent(out) << "for(_idx = 0; _idx < " << ((fidMax + EDF_FID_FACTOR - 1)/EDF_FID_FACTOR) << "; _idx++){" << endl;
				indent_up();
					indent(out) << "_pFid[_idx] = 0; "	<< endl;
				indent_down();
			indent(out) << "}"	<< endl;

			#endif
			
			for (m_iter = members.begin(); m_iter != members.end(); ++m_iter) {
				Edf_Field * tFid;
				Edf_Type  * ttf;
			
				tFid = *m_iter;
				ttf  = get_true_type(tFid->get_type());
				fn = tFid->get_name();

				if(tFid->is_sequence()){
					// Add the Comment 
					sComment = "Init the Sub: A Element: " + fn;
					gen_comment(out, sComment);

					CLUE_OFF_IDX_APARAM();
					indent(out) << "_pFid = " << ELEM_APTR() << ";" << endl;
					
					indent(out) << "_idx = " << ((tFid->get_repeat() + EDF_FID_FACTOR - 1)/EDF_FID_FACTOR) << ";" << endl;
					indent(out) << "do{ " << endl;
					indent_up();
						indent(out) << "*_pFid = 0;"	<< endl;
						indent(out) << "++_pFid;   "	<< endl;
					indent_down();
					indent(out) << "}while(--_idx);"	<< endl;
					
					#if 0
					indent(out) << "_xEdfZero32(_pFid, " << ((tFid->get_repeat() + EDF_FID_FACTOR - 1)/EDF_FID_FACTOR) << ");" << endl;
					#endif
				}
			}
	}

	indent(out) << endl;
	indent(out) << "return;"<< endl;
	indent_down();	
	out <<	"}"	<<	endl	<<	endl;	
	
	return;
}

void 	Edf_GenerC::gen_pack_encode(ofstream& out, Edf_Pack * tp, bool is_ref, bool is_pointer) 
{
	string 		ttn;	
	bool 		unk;

	unk = false;
	ttn = get_typename(tp);
	if(ttn == ""){
		unk = true;
	}
	
	const vector<Edf_Field*>&			members= tp->get_members();
	vector<Edf_Field*>::const_iterator	m_iter;
	
	int fidMax = 0;
	/* We Order all the Members */
	for (m_iter = members.begin(); m_iter != members.end(); ++m_iter) {
		Edf_Field * tFid;
		int 		fid;
		tFid = *m_iter;
		fid  = tFid->get_fid();
		if((fid >0) && (fid < 127)){
			if(fid > fidMax)
				fidMax = fid;
		}
		else{
			throw "Fid Error:" + tFid->get_name();
		}
	}

	fidMax += 1;
	/* Gen Enc Table */		
	if(unk == false){
		// Add the Comment 
		string sComment = "<Encode> Pack: ";
		sComment += ttn;
		gen_comment(out, sComment);

		out << "_sInt32 _xEncode_"	<< ttn	<< "(" << ttn	<< " * _xPtr, _uInt8 * _oPtr, _sInt32 _oLen, _sInt32 * _oStatus){" <<	endl;
		indent_up();
			indent(out) << "_uInt32 *_pFid = _xPtr->_presence._fid;" << endl;
			indent(out) << "_sInt32 (** _pFTbl)() = _gXTblEnc_" << ttn << ";" << endl;
	}
	else{
		CLUE_PREFIX();
		// Add the Comment 
		string sComment = "<Encode> Pack: ";
		sComment += prefix;
		gen_comment(out, sComment);

		out << "static _sInt32 _xEncode_"	<< prefix 	<< "(void * _xPtr, _uInt8 * _oPtr, _sInt32 _oLen, _sInt32 * _oStatus){"	<<	endl;
		indent_up();
			CLUE_OFF_IDX_PBEG();
			indent(out) << "_uInt32 *_pFid = " << ELEM_PPTR() << ";" << endl;
			indent(out) << "_sInt32 (** _pFTbl)() = _gXTblEnc_" << prefix << ";" << endl;
	}

		indent(out) << "_sInt32 _idx, _len, _sum;"	<< endl;
		indent(out) << "if( _xPtr && _oPtr && (EDF_PACK_MIN <= _oLen)){"		<< endl;
		indent_up();
			indent(out) << "for(_idx = 1, _len = 0, _sum = 0; _idx < " << fidMax << "; _idx++){" << endl;
			indent_up();
				indent(out) << "if((_pFid[_idx/" <<EDF_FID_FACTOR<<"]  & (0X01 << (_idx %" << EDF_FID_FACTOR << "))) && (_pFTbl[_idx] != NULL)){"	<< endl; 
				indent_up();
					indent(out) << "_len = _pFTbl[_idx](_xPtr, _oPtr, _oLen, _oStatus);" << endl;
					indent(out) << "if(_len < 0){" <<endl;
					indent_up();
						indent(out) << "return -1;" << endl;
					indent_down();
					indent(out) << "}" << endl;
					indent(out) << "_sum += _len;" << endl;
					indent(out) << "_oPtr+= _len;" << endl;
					indent(out) << "_oLen-= _len;" << endl;
				indent_down();
				indent(out) << "}"	<< endl;
			indent_down();
			indent(out) << "}"	<< endl;
			indent(out) << "return _sum;" << endl;
		indent_down();
		indent(out) << "}"			<< endl;
		/* OOM */
		indent(out) << "if(_oStatus)  (*_oStatus) |= _EDF_E_OOM; " << endl;
		indent(out) << "return 0;" << endl;
		
	indent_down();	
	out <<	"}"	<<	endl 	<<	endl;	
	
	return;
}


void 	Edf_GenerC::gen_pack_sizeof(ofstream& out, Edf_Pack * tp, bool is_ref, bool is_pointer) 
{
	string 		ttn;	
	bool 		unk;

	unk = false;
	ttn = get_typename(tp);
	if(ttn == ""){
		unk = true;
	}
	
	const vector<Edf_Field*>&			members= tp->get_members();
	vector<Edf_Field*>::const_iterator	m_iter;
	
	int fidMax = 0;
	/* We Order all the Members */
	for (m_iter = members.begin(); m_iter != members.end(); ++m_iter) {
		Edf_Field * tFid;
		int 		fid;
		tFid = *m_iter;
		fid  = tFid->get_fid();
		if((fid >0) && (fid < 127)){
			if(fid > fidMax)
				fidMax = fid;
		}
		else{
			throw "Fid Error:" + tFid->get_name();
		}
	}

	fidMax += 1;
	/* Gen Enc Table */		
	if(unk == false){
		// Add the Comment 
		string sComment = "<SizeOf> Pack: ";
		sComment += ttn;
		gen_comment(out, sComment);

		out << "_sInt32 _xSizeOf_"	<< ttn	<< "(" << ttn	<< " * _xPtr){" <<	endl;
		indent_up();
			indent(out) << "_uInt32 *_pFid = _xPtr->_presence._fid;" << endl;
			indent(out) << "_sInt32 (** _pFTbl)() = _gXTblSz_" << ttn << ";" << endl;
	}
	else{
		CLUE_PREFIX();
		// Add the Comment 
		string sComment = "<SizeOf> Pack: ";
		sComment += prefix;
		gen_comment(out, sComment);

		out << "static _sInt32 _xSizeOf_"	<< prefix 	<< "(void * _xPtr){"	<<	endl;
		indent_up();
			CLUE_OFF_IDX_PBEG();
			indent(out) << "_uInt32 *_pFid = " << ELEM_PPTR() << ";" << endl;
			indent(out) << "_sInt32 (** _pFTbl)() = _gXTblSz_" << prefix << ";" << endl;
	}

		indent(out) << "_sInt32 _idx, _len, _sum;"	<< endl;
		indent(out) << "for(_idx = 1, _len = 0, _sum = 0; _idx < " << fidMax << "; _idx++){" << endl;
			indent_up();
				indent(out) << "if((_pFid[_idx/" <<EDF_FID_FACTOR<<"]  & (0X01 << (_idx %" << EDF_FID_FACTOR << "))) && (_pFTbl[_idx] != NULL)){"	<< endl; 
				indent_up();
					indent(out) << "_len  = _pFTbl[_idx](_xPtr);" << endl;
					indent(out) << "_sum += _len;" << endl;
				indent_down();
				indent(out) << "}"	<< endl;
		indent_down();
		indent(out) << "}"			<< endl;
		indent(out) << "return _sum;"		<< endl;
		
	indent_down();	
	out <<	"}"	<<	endl 	<<	endl;	
	
	return;
}


void 	Edf_GenerC::gen_pack_decode(ofstream& out, Edf_Pack * tp, bool is_ref, bool is_pointer) 
{
	string 		ttn;	
	bool 		unk;

	unk = false;
	ttn = get_typename(tp);
	if(ttn == ""){
		unk = true;
	}
	
	const vector<Edf_Field*>&			members= tp->get_members();
	vector<Edf_Field*>::const_iterator	m_iter;
	
	int fidMax = 0;
	/* We Order all the Members */
	for (m_iter = members.begin(); m_iter != members.end(); ++m_iter) {
		Edf_Field * tFid;
		int 		fid;
		tFid = *m_iter;
		fid  = tFid->get_fid();
		if((fid >0) && (fid < 127)){
			if(fid > fidMax)
				fidMax = fid;
		}
		else{
			throw "Fid Error:" + tFid->get_name();
		}
	}

	fidMax += 1;
	/* Gen Enc Table */		
	if(unk == false){
		// Add the Comment 
		string sComment = "<Decode> Pack: ";
		sComment += ttn;
		gen_comment(out, sComment);

		out << "_sInt32 _xDecode_"	<< ttn	<< "(" << ttn	<< " * _xPtr, _uInt8 * _oPtr, _sInt32 _oLen, _sInt32 * _oStatus){" <<	endl;
		indent_up();
			indent(out) << "_uInt8	_iFid;" << endl;
			indent(out) << "_sInt32 _len, _sum;"	<< endl;
			#if 0
			indent(out) << "_uInt32 * _pFid = _xPtr->_presence._fid;" << endl;
			#endif
			indent(out) << "_sInt32 (** _pFTbl)() = _gXTblDec_" << ttn << ";" << endl;
			indent(out) << "if(_xPtr && _oPtr){" << endl;
			indent_up();
			indent(out) << "_xInit_" << ttn << "(_xPtr);" << endl;
	}
	else{
		CLUE_PREFIX();
		// Add the Comment 
		string sComment = "<Decode> Pack: ";
		sComment += prefix;
		gen_comment(out, sComment);

		out << "static _sInt32 _xDecode_"	<< prefix	<< "(void * _xPtr, _uInt8 * _oPtr, _sInt32 _oLen, _sInt32 * _oStatus){"	<<	endl;
		indent_up();
			indent(out) << "_uInt8	_iFid;" << endl;
			indent(out) << "_sInt32 _len, _sum;"	<< endl;
			#if 0
			CLUE_OFF_IDX_PBEG();
			indent(out) << "_uInt32 * _pFid = " << ELEM_PPTR() << ";" << endl;
			#endif
			indent(out) << "_sInt32 (** _pFTbl)() = _gXTblDec_" << prefix << ";" << endl;
			indent(out) << "if(_xPtr && _oPtr){" << endl;
			indent_up();
			indent(out) << "_xInit_" << prefix << "(_xPtr);" << endl;
	}
			indent(out) << "_sum = 0;" << endl;

			indent(out) << "while(_oLen > 0){" << endl;
			indent_up();
				indent(out) << "_iFid = (*(_oPtr)) >> 1;" 	<< endl;
				#if 0
				indent(out) << "_iType = *(_oPtr + 1);" << endl;
				#endif
				indent(out) << "if((_iFid < " << fidMax << ") && (_pFTbl[_iFid] != NULL)){" << endl;
				indent_up();
					indent(out) << "_len = _pFTbl[_iFid](_xPtr, _oPtr, _oLen, _oStatus);" << endl;
					indent(out) << "if(_len <= 0){" <<endl;
					indent_up();
						indent(out) << "if(_oStatus)  (*_oStatus) |= _EDF_E_CRT; " << endl;
						indent(out) << "return -1;" << endl;
					indent_down();
					indent(out) << "}" << endl;
				indent_down();
				indent(out) << "} else {"	<< endl;
				indent_up();
					indent(out) << "if(_oStatus)  (*_oStatus) |= _EDF_E_UNK; " << endl;
					indent(out) << "_len = " << _dec_unknown <<"(_oPtr, _oLen);" << endl;
					indent(out) << "if(_len <= 0){" <<endl;
					indent_up();
						indent(out) << "if(_oStatus)  (*_oStatus) |= _EDF_E_CRT; " << endl;
						indent(out) << "return -1;" << endl;
					indent_down();
					indent(out) << "}" << endl;
				indent_down();
				indent(out) << "}" << endl;
				
				indent(out) << "_sum += _len;" << endl;
				indent(out) << "_oPtr+= _len;" << endl;
				indent(out) << "_oLen-= _len;" << endl;

			indent_down();
			indent(out) << "}"		<< endl;
			indent(out) << "return _sum;" << endl;
			
			indent_down();	
			indent(out) << "}" << endl;
			indent(out) << "return -1;" << endl;	
			
		indent_down();	
		out <<	"}"		
			<<	endl	
			<<	endl;	
	
	return;
}


void 	Edf_GenerC::gen_pack_def(ofstream& out, Edf_Pack* tp, bool is_ref, bool is_pointer) 
{
	int 	fid, fidMax;

	// Add the Comment 
	string sComment = "Prototype Definition of Pack: ";
	sComment += tp->get_name();
	gen_comment(out, sComment);

	if(!is_ref){
		indent(out) << "typedef struct " << tp->get_name() << " {" << endl;
	}
	else{
		indent(out) << "struct " << tp->get_name() << " {" << endl;
	}
	indent_up();


	const vector<Edf_Field*>& 			members = tp->get_members();
	vector<Edf_Field*>::const_iterator 	m_iter;

	/* Output the Presence Fields */
	indent(out) << "struct " << " {" << endl;
	indent_up();
	fidMax = 0;
	for (m_iter = members.begin(); m_iter != members.end(); ++m_iter) {
		fid = (*m_iter)->get_fid();
		if( fid > fidMax)
			fidMax = fid;
	}
	fidMax += 1;
	indent(out) << "_uInt32 _fid[" << (fidMax + EDF_FID_FACTOR - 1)/EDF_FID_FACTOR << "];" <<endl;
	
	for (m_iter = members.begin(); m_iter != members.end(); ++m_iter) {
		gen_field_presence(out, *m_iter, (*m_iter)->get_repeat());
	}
	indent_down();

	indent(out) << "}_presence;" << endl 	<< endl;

	if(tp->is_union()){
		indent(out) << "union " << " {" << endl;
		indent_up();
	}

	for (m_iter = members.begin(); m_iter != members.end(); ++m_iter) {
		gen_field_decl(out, *m_iter, is_pointer, false, (*m_iter)->get_repeat());
	}
	
	#if 0
	bool has_opt_fid = true;
	for (m_iter = members.begin(); m_iter != members.end(); ++m_iter) {
		if ((*m_iter)->get_req() != Edf_Field::T_REQUIRED)
		has_opt_fid = true;
	}
	#endif
	
	if(tp->is_union()){
		indent_down();
		indent(out) <<	"};" << endl;
	}
	
	indent_down();
	indent(out) <<	"}";
	
	if(!is_ref){
		out << tp->get_name() << ";" << endl << endl;
	}
}


void	Edf_GenerC::gen_pack_var_if(ofstream& out, Edf_Pack* tp, bool is_ref, bool is_pointer) 
{
	string	ttn;
	
	if(!is_ref){
		// Add the Comment 
		string sComment = "Global Interface Var of Pack: ";
		sComment += tp->get_name();
		gen_comment(out, sComment);

		indent(out) << "static " << "_I_" << tp->get_name() 
					<< " _gI_" << tp->get_name() << " = {"
					<< endl;
		indent_up();
		
		const vector<Edf_Field*>&			members = tp->get_members();
		vector<Edf_Field*>::const_iterator	m_iter;
		for (m_iter = members.begin(); m_iter != members.end(); ++m_iter) {
			gen_field_var_if(out, (Edf_Type *)tp, *m_iter, is_pointer);
			out << endl;
		}
		
		/* Encode & Decode */
		bool		unk;
		unk = false;
		ttn = get_typename(tp);
		if(ttn == ""){
			unk = true;
		}
		if(unk == false){
			string vFid, pFid;
			indent(out) << "_xEncode_"	<< ttn		<< ","			<<	endl;
			indent(out) << "_xDecode_"	<< ttn		<< ","			<<	endl;
			indent(out) << "_xSizeOf_"	<< ttn		<< ","			<<	endl;
			indent(out) << "_xInit_"	<< ttn		<<	endl;
		}
		else{
			CLUE_PREFIX();
			indent(out) << "_xEncode_"	<< prefix	<< ","			<<	endl;
			indent(out) << "_xDecode_"	<< prefix	<< ","			<<	endl;
			indent(out) << "_xSizeOf_"	<< ttn		<< ","			<<	endl;
			indent(out) << "_xInit_"	<< prefix	<<	endl;
		}
		
		indent_down();
		indent(out) <<"};" << endl	<< endl;
	}
	
	else{
		indent(out) << " {" << endl;
		indent_up();
		
		const vector<Edf_Field*>&			members = tp->get_members();
		vector<Edf_Field*>::const_iterator	m_iter;
		
		for (m_iter = members.begin(); m_iter != members.end(); ++m_iter) {
			gen_field_var_if(out, (Edf_Type *)tp, *m_iter, is_pointer);
			out << endl;
		}
	}
	
}

void	Edf_GenerC::gen_pack_init_if(ofstream& out, Edf_Pack* tp, bool is_ref, bool is_pointer) 
{
	string ttn;
	if(!is_ref){
		ttn = get_typename(tp);
		if(ttn != ""){
			// Add the Comment 
			string sComment = "Global Interface Init of Pack: ";
			sComment += tp->get_name();
			gen_comment(out, sComment);
			indent(out) << "void _I_" << tp->get_name() << "_Init(_I_" << ttn << "** _ppIf){"  << endl;
			indent_up();
				indent(out) << "if(_ppIf){" 		<< endl;
					indent_up();
					indent(out) << "*_ppIf = &_gI_" 	<< ttn << ";" << endl;
					indent_down();
				indent(out)<< "}" << endl;
			indent_down();
			indent(out)<< "}" << endl 
				<< endl
				<< endl;
		}
	}
}


void 	Edf_GenerC::gen_pack_ftbl(ofstream& out, Edf_Pack * tp, bool is_ref, bool is_pointer) 
{
	struct{
		Edf_Field * tf;		
	}table[EFID_MAX];

	string 		ttn;	
	bool 		unk;

	unk = false;
	ttn = get_typename(tp);
	if(ttn == ""){
		unk = true;
	}
	
	const vector<Edf_Field*>&			members= tp->get_members();
	vector<Edf_Field*>::const_iterator	m_iter;
	
	int fidMax = 0;
	for(int i=0; i<EFID_MAX; i++){
		table[i].tf = NULL;
	}
	/* We Order all the Members */
	for (m_iter = members.begin(); m_iter != members.end(); ++m_iter) {
		Edf_Field * tFid;
		int 		fid;
		tFid = *m_iter;
		fid  = tFid->get_fid();
		if((fid >0) && (fid < 127)){
			if(fid > fidMax)
				fidMax = fid;
			table[fid].tf = tFid;
		}
		else{
			throw "Fid Error:" + tFid->get_name();
		}
	}
	fidMax +=1;
	/* Gen Enc Table */		
	if(unk == false){
		// Add the Comment 
		string sComment = "Function Table of Pack: ";
		sComment += ttn;
		gen_comment(out, sComment);

		out << "static _sInt32 (* _gXTblEnc_"	<< ttn 	<< "[" << fidMax << "])()={"			<<	endl;
		indent_up();
		for(int i=0; i<fidMax; i++){
			if(table[i].tf == NULL){
				indent(out) << "NULL";
			}else{
				if(table[i].tf->is_sequence()){
					indent(out) <<	"_xEncAt_" << ttn 	<< "_" << table[i].tf->get_name();
				}
				else{
					indent(out) <<	"_xEnc_" 	<< ttn 	<< "_" << table[i].tf->get_name();
				}
			}
			if(i != (fidMax-1)){
				out << ", " << endl;
			}
			else{
				out << endl;
			}
		}
		indent_down();
		out <<	"};" 
			<<	endl 
			<<	endl;
	}
	else{
		CLUE_PREFIX();
		// Add the Comment 
		string sComment = "Function Table of Pack: ";
		sComment += prefix;
		gen_comment(out, sComment);

		out << "static _sInt32 (* _gXTblEnc_"		<< prefix 	<< "[" << fidMax << "])()={"	<<	endl;
		indent_up();
		for(int i=0; i<fidMax; i++){
			if(table[i].tf == NULL){
				indent(out) << "NULL";
			}else{
				if(table[i].tf->is_sequence()){
					indent(out) << 	"_xEncAt_" 	<< prefix  	<< "_"<< table[i].tf->get_name();
				}
				else{
					indent(out) <<	"_xEnc_" 	<< prefix  	<< "_"<< table[i].tf->get_name();
				}
			}
			if(i != (fidMax-1)){
				out << ", " << endl;
			}
			else{
				out << endl;
			}
		}
		indent_down();
		out <<	"};" 
			<<	endl 
			<<	endl;
	}
	
	/* Gen Dec Table */		
	if(unk == false){
		out << "static _sInt32 (* _gXTblDec_"		<< ttn 	<< "[" << fidMax << "])()={"		<<	endl;
		indent_up();
		for(int i=0; i<fidMax; i++){
			if(table[i].tf == NULL){
				indent(out) << "NULL";
			}else{
				if(table[i].tf->is_sequence()){
					indent(out) <<	"_xDecAt_" << ttn 	<< "_" << table[i].tf->get_name();
				}
				else{
					indent(out) <<	"_xDec_" 	<< ttn 	<< "_" << table[i].tf->get_name();
				}
			}
			if(i != (fidMax-1)){
				out << ", " << endl;
			}
			else{
				out << endl;
			}
		}
		indent_down();
		out <<	"};" 
			<<	endl 
			<<	endl;
	}
	else{
		CLUE_PREFIX();
		out << "static _sInt32 (* _gXTblDec_"		<< prefix 	<< "[" << fidMax << "])()={"	<<	endl;
		indent_up();
		for(int i=0; i<fidMax; i++){
			if(table[i].tf == NULL){
				indent(out) << "NULL";
			}else{
				if(table[i].tf->is_sequence()){
					indent(out) << 	"_xDecAt_" 	<< prefix  	<< "_"<< table[i].tf->get_name();
				}
				else{
					indent(out) <<	"_xDec_" 	<< prefix  	<< "_"<< table[i].tf->get_name();
				}
			}
			if(i != (fidMax-1)){
				out << ", " << endl;
			}
			else{
				out << endl;
			}
		}
		indent_down();
		out <<	"};" 
			<<	endl 
			<<	endl;
	}

	/* Gen Sz Table */		
	if(unk == false){
		out << "static _sInt32 (* _gXTblSz_"		<< ttn 	<< "[" << fidMax << "])()={"		<<	endl;
		indent_up();
		for(int i=0; i<fidMax; i++){
			if(table[i].tf == NULL){
				indent(out) << "NULL";
			}else{
				if(table[i].tf->is_sequence()){
					indent(out) <<	"_xSizeAt_" << ttn 	<< "_" << table[i].tf->get_name();
				}
				else{
					indent(out) <<	"_xSize_" 	<< ttn 	<< "_" << table[i].tf->get_name();
				}
			}
			if(i != (fidMax-1)){
				out << ", " << endl;
			}
			else{
				out << endl;
			}
		}
		indent_down();
		out <<	"};" 
			<<	endl 
			<<	endl;
	}
	else{
		CLUE_PREFIX();
		out << "static _sInt32 (* _gXTblSz_"		<< prefix 	<< "[" << fidMax << "])()={"	<<	endl;
		indent_up();
		for(int i=0; i<fidMax; i++){
			if(table[i].tf == NULL){
				indent(out) << "NULL";
			}else{
				if(table[i].tf->is_sequence()){
					indent(out) << 	"_xSizeAt_" 	<< prefix  	<< "_"<< table[i].tf->get_name();
				}
				else{
					indent(out) <<	"_xSize_" 		<< prefix  	<< "_"<< table[i].tf->get_name();
				}
			}
			if(i != (fidMax-1)){
				out << ", " << endl;
			}
			else{
				out << endl;
			}
		}
		indent_down();
		out <<	"};" 
			<<	endl 
			<<	endl;
	}
	
	return;
}

/*------------------------------------------------------------------------------------------------------------
** Generate the Method Union
*/
void	Edf_GenerC::gen_set_Pack(ofstream& out, Edf_Type * tt, Edf_Field * tf, bool unk)
{
	ostringstream nss;
	nss 	<<	tf->get_fid()/EDF_FID_FACTOR;
	string nFid = nss.str();	
	string tn, fn, tfn; 
	fn		= tf->get_name();	
	tfn 	= get_typename(get_true_type(tf->get_type()));
	if(unk == false){
		string vFid, pFid;
		tn	 = tt->get_name();
		vFid = "_xPtr->" + fn;
		pFid = "_xPtr->_presence._fid[" + nFid	+  "]";

		out << "static _sInt32 _xSet_" 	<< tn	<< "_"		<< fn	<< "(" << tn	<< " * _xPtr){"		<<	endl;

		indent_up();
		if(get_type_id(get_true_type(tt)) == EDF_T_UNION){
			indent(out) << "_xClean_" << tn << "(_xPtr);" << endl;
		}
		indent(out) << pFid <<	" |= (0X01 << " << tf->get_fid()%EDF_FID_FACTOR << ");" 				<< endl;
	}
	else{
		CLUE_PREFIX();
		tn	 = "void ";
		out << "static _sInt32 _xSet_" 	<< prefix<< "_"		<< fn	<< "(" << tn	<< " * _xPtr){"		<<	endl;

		indent_up();
		CLUE_OFF_P();
		if(get_type_id(get_true_type(tt)) == EDF_T_UNION){
			indent(out) << "_xClean_" << prefix << "(_xPtr);" << endl;
		}
		indent(out) << ELEM_P() 	<<	" |= (0X01 << " << tf->get_fid()%EDF_FID_FACTOR << ");" 			<< endl;
	}
	indent(out) << "return (0);" << endl;
	indent_down();
	out <<	"}"  
		<<	endl 
		<<	endl;
}

void	Edf_GenerC::gen_exist_Pack(ofstream& out, Edf_Type * tt, Edf_Field * tf, bool unk)
{	
	ostringstream nss;
	nss 	<<	tf->get_fid()/EDF_FID_FACTOR;
	string nFid = nss.str();	
	string tn, fn, tfn; 
	fn		= tf->get_name();	
	tfn 	= get_typename(get_true_type(tf->get_type()));
	if(unk == false){
		string vFid, pFid;
		tn	 = tt->get_name();
		vFid = "_xPtr->" + fn;
		pFid = "_xPtr->_presence._fid[" + nFid	+  "]";

		out << "static _sInt32 _xExist_" 	<< tn		<< "_"		<< fn	<< "(" << tn	<< " * _xPtr){"	<<	endl;

		indent_up();
		indent(out) << "if(" << pFid << " & (0X01 << "	<< tf->get_fid()%EDF_FID_FACTOR << ")){" 			<< endl;
	}
	else{
		CLUE_PREFIX();
		tn	 = "void ";
		out << "static _sInt32 _xExist_" 	<< prefix	<< "_"		<< fn	<< "(" << tn	<< " * _xPtr){"	<<	endl;

		indent_up();
		CLUE_OFF_P();
		indent(out) << "if(" << ELEM_P() << " & (0X01 << "	<< tf->get_fid()%EDF_FID_FACTOR << ")){" 		<< endl;
	}
		indent_up();
		indent(out) << "return (1);"		<< endl;
		indent_down();
		indent(out) << "}" 					<< endl;
	indent(out) << "return (0);" 		<< endl;
	indent_down();	
	out <<	"}" 	
		<<	endl 	
		<<	endl;	
}

void	Edf_GenerC::gen_clear_Pack(ofstream& out, Edf_Type * tt, Edf_Field * tf, bool unk)
{	
	ostringstream nss;
	nss 	<<	tf->get_fid()/EDF_FID_FACTOR;
	string nFid = nss.str();	
	string tn, fn, tfn; 
	fn		= tf->get_name();	
	tfn 	= get_typename(get_true_type(tf->get_type()));
	if(unk == false){
		string vFid, pFid;
		tn	 = tt->get_name();
		vFid = "_xPtr->" + fn;
		pFid = "_xPtr->_presence._fid[" + nFid	+  "]";

		out << "static void _xClear_" 	<< tn		<< "_"		<< fn	<< "(" << tn	<< " * _xPtr){"	<<	endl;

		indent_up();
		indent(out) << pFid << " &= ~(0X01 << "	<< tf->get_fid()%EDF_FID_FACTOR << ");" 			<< endl;
	}
	else{
		CLUE_PREFIX();
		tn	 = "void ";
		out << "static void _xClear_" 	<< prefix	<< "_"		<< fn	<< "(" << tn	<< " * _xPtr){"	<<	endl;

		indent_up();
		CLUE_OFF_P();
		indent(out) << ELEM_P() << " &= ~(0X01 << "	<< tf->get_fid()%EDF_FID_FACTOR << ");" 		<< endl;
	}
	indent(out) << "return;" 		<< endl;
	indent_down();	
	out <<	"}" 	
		<<	endl 	
		<<	endl;	
}


void	Edf_GenerC::gen_enc_Pack(ofstream& out, Edf_Type * tt, Edf_Field * tf, bool unk)
{
	ostringstream nss;
	nss 	<<	tf->get_fid()/EDF_FID_FACTOR;
	string 	nFid = nss.str();	
	string 	tn, fn, tfn; 	
	Edf_Type * 	ttf;
	
	ttf 	= get_true_type(tf->get_type());
	fn		= tf->get_name();	
	tfn 	= get_typename(get_true_type(tf->get_type()));
	if(tfn == ""){
		tfn = "void ";
	}
	if(unk == false){
		string vFid;
		tn	 = tt->get_name();
		vFid = "_xPtr->" + fn;
		
		out << "static _sInt32 _xEnc_"	<< tn	<< "_"	
			<< fn	<< "(" << tn	<< " * _xPtr, _uInt8 * _oPtr, _sInt32 _oLen, _sInt32 * _oStatus){"	<<	endl;
		indent_up();
			if(tfn != "void "){
				indent(out) << "_sInt32 (* _fEncode)() = (_sInt32 (*)())_xEncode_" 	<< tfn << ";"					<< endl;
			}else{
				indent(out) << "_sInt32 (* _fEncode)() = (_sInt32 (*)())_xEncode_" 	<< tn << "_" << fn << ";"		<< endl;
			}
			indent(out) << tfn << " * _iPtr = (" << tfn <<" *)(&(" << vFid << "));" << endl;
	}
	else{
		CLUE_PREFIX();
		tn	 = "void ";
		out << "static _sInt32 _xEnc_"	<< prefix<< "_"	<< fn	<< "(" << tn	<< " * _xPtr, _uInt8 * _oPtr, _sInt32 _oLen, _sInt32 * _oStatus){"	<<	endl;
		indent_up();
			if(tfn != "void "){
				indent(out) << "_sInt32 (* _fEncode)() = (_sInt32 (*)())_xEncode_" 	<< tfn << ";"					<< endl;
			}else{
				indent(out) << "_sInt32 (* _fEncode)() = (_sInt32 (*)())_xEncode_" 	<< prefix	<< "_"<< fn << ";"	<< endl;
			}
			CLUE_OFF_V();
			indent(out) << tfn << " * _iPtr = (" << tfn <<" *)(" << ELEM_VPTR() << ");"<< endl;
	}
		indent(out) <<"_sInt32 _len;" 		<< endl;
		indent(out) << "if( EDF_PACK_MIN <= _oLen){"		<< endl;
		indent_up();
			indent(out) << "_len = _fEncode(_iPtr, _oPtr + 6, _oLen - 6, _oStatus);" << endl;
			indent(out) << "if(_len > 0){" 		<< endl;
			indent_up();
				indent(out) << "*_oPtr = "			<< (tf->get_fid()<< 0X01)	<< ";"		<< endl;
				indent(out) << "*(_oPtr + 1) = "	<< (EDF_T_PACK << 0X04)<< ";"		<< endl;
				indent(out) << "*((_uInt32 *)(_oPtr + 2)) = _len + 6;"	<< endl;
				_ENDVAL_HTONL("*((_uInt32 *)(_oPtr + 2))");
				indent(out) << "return (_len + 6);" << endl;
			indent_down();
			indent(out) << "}" << endl;
			indent(out) << "return _len;" << endl;
		indent_down();
		indent(out) << "}" << endl;
		/* OOM */
		// Add the Comment 
		string sComment = "Check OOM : ";
		sComment += tf->get_name();
		gen_comment(out, sComment);
		
		indent(out) << "if(_oStatus)  (*_oStatus) |= _EDF_E_OOM; " << endl;
		indent(out) << "return 0;" << endl;
		
	indent_down();
	out <<	"}" 
		<<	endl 
		<<	endl;
}


void	Edf_GenerC::gen_size_Pack(ofstream& out, Edf_Type * tt, Edf_Field * tf, bool unk)
{
	ostringstream nss;
	nss 	<<	tf->get_fid()/EDF_FID_FACTOR;
	string 	nFid = nss.str();	
	string 	tn, fn, tfn; 	
	Edf_Type * 	ttf;
	
	ttf 	= get_true_type(tf->get_type());
	fn		= tf->get_name();	
	tfn 	= get_typename(get_true_type(tf->get_type()));
	if(tfn == ""){
		tfn = "void ";
	}
	if(unk == false){
		string vFid;
		tn	 = tt->get_name();
		vFid = "_xPtr->" + fn;
		
		out << "static _sInt32 _xSize_"	<< tn	<< "_"	
			<< fn	<< "(" << tn	<< " * _xPtr){"	<<	endl;
		indent_up();
			if(tfn != "void "){
				indent(out) << "_sInt32 (* _fSizeOf)() = (_sInt32 (*)())_xSizeOf_" 	<< tfn << ";"					<< endl;
			}else{
				indent(out) << "_sInt32 (* _fSizeOf)() = (_sInt32 (*)())_xSizeOf_" 	<< tn << "_" << fn << ";"		<< endl;
			}
			indent(out) << tfn << " * _iPtr = (" << tfn <<" *)(&(" << vFid << "));" << endl;
	}
	else{
		CLUE_PREFIX();
		tn	 = "void ";
		out << "static _sInt32 _xSize_"	<< prefix<< "_"	
			<< fn	<< "(" << tn	<< " * _xPtr){"	<<	endl;
		indent_up();
			if(tfn != "void "){
				indent(out) << "_sInt32 (* _fSizeOf)() = (_sInt32 (*)())_xSizeOf_" 	<< tfn << ";"					<< endl;
			}else{
				indent(out) << "_sInt32 (* _fSizeOf)() = (_sInt32 (*)())_xSizeOf_" 	<< prefix	<< "_"<< fn << ";"	<< endl;
			}
			CLUE_OFF_V();
			indent(out) << tfn << " * _iPtr = (" << tfn <<" *)(" << ELEM_VPTR() << ");"<< endl;
	}
		indent(out) <<"_sInt32 _len;" 		<< endl;

		indent(out) << "_len = _fSizeOf(_iPtr);" << endl;
		indent(out) << "if(_len > 0){" 		<< endl;
		indent_up();
			indent(out) << "return (_len + 6);" << endl;
		indent_down();
		indent(out) << "}" << endl;
		indent(out) << "return 0;" << endl;
		
	indent_down();
	out <<	"}" 
		<<	endl 
		<<	endl;
}


void	Edf_GenerC::gen_dec_Pack(ofstream& out, Edf_Type * tt, Edf_Field * tf, bool unk)
{
	ostringstream nss;
	nss 	<<	tf->get_fid()/EDF_FID_FACTOR;
	string 	nFid = nss.str();	
	string 	tn, fn, tfn; 	
	Edf_Type * 	ttf;
	
	ttf 	= get_true_type(tf->get_type());
	fn		= tf->get_name();	
	tfn 	= get_typename(get_true_type(tf->get_type()));
	if(tfn == ""){
		tfn = "void ";
	}
	string pFid;
	if(unk == false){
		string vFid;
		tn	 = tt->get_name();
		vFid = "_xPtr->" + fn;

		out << "static _sInt32 _xDec_"	<< tn	<< "_"	
			<< fn	<< "(" << tn	<< " * _xPtr, _uInt8 * _oPtr, _sInt32 _oLen, _sInt32 * _oStatus){"	<<	endl;
		indent_up();
			if(tfn != "void "){
				indent(out) << "_sInt32 (* _fDecode)() = (_sInt32 (*)())_xDecode_" 	<< tfn << ";"					<< endl;
			}else{
				indent(out) << "_sInt32 (* _fDecode)() = (_sInt32 (*)())_xDecode_" 	<< tn << "_" << fn << ";"		<< endl;
			}
			indent(out) << tfn << " * _iPtr = (" << tfn <<" *)(&(" << vFid << "));" << endl;
			pFid = "_xPtr->_presence._fid[" + nFid	+  "]";
	}
	else{
		CLUE_PREFIX();
		tn	 = "void ";
		out << "static _sInt32 _xDec_"	<< prefix<< "_"
			<< fn	<< "(" << tn	<< " * _xPtr, _uInt8 * _oPtr, _sInt32 _oLen, _sInt32 * _oStatus){"	<<	endl;
		indent_up();
			if(tfn != "void "){
				indent(out) << "_sInt32 (* _fDecode)() = (_sInt32 (*)())_xDecode_" 	<< tfn << ";"					<< endl;
			}else{
				indent(out) << "_sInt32 (* _fDecode)() = (_sInt32 (*)())_xDecode_" 	<< prefix<< "_"	<< fn << ";"	<< endl;
			}
			CLUE_OFF_V(); 	CLUE_OFF_P();
			indent(out) << tfn << " * _iPtr = (" << tfn <<" *)(" << ELEM_VPTR() << ");"<< endl;
			pFid = ELEM_P();
	}
		indent(out) << "_uInt8	_iType;" 		<< endl;
		indent(out) << "_sInt32	_iLen, _dLen;" 		<< endl;
		indent(out) << "_iType = *(_oPtr + 1);" << endl;
		_ENDVAL_NTOHL("*((_sInt32 *)(_oPtr + 2))");
		indent(out) << "_iLen  = *((_sInt32 *)(_oPtr + 2));" << endl;
		indent(out) << "if((_iType >> 4) == " 	<< EDF_T_PACK << "){" << endl;
		indent_up();
			indent(out) << "if(_iLen > _oLen){" << endl;
			indent_up();
					// Add the Comment 
					string sComment = "Check OOM : ";
					sComment += tf->get_name();
					gen_comment(out, sComment);
					
					indent(out) << "if(_oStatus)  (*_oStatus) |= (_EDF_E_CRT | _EDF_E_OOM); " << endl;
					indent(out) << "return -1;" << endl;
			indent_down();
			indent(out) << "}" << endl;
			
			indent(out) << "_dLen = _fDecode(_iPtr, _oPtr + 6, _iLen - 6, _oStatus); " << endl;
			#if 1
			indent(out) << "if( (_dLen + 6) == _iLen ){" 	<< endl;
			#else
			indent(out) << "if( _dLen >=0 ){" 	<< endl;
			#endif
			indent_up();
				indent(out) << pFid << " |= (0X01 << " << tf->get_fid()%EDF_FID_FACTOR << ");"		<< endl;
				indent(out) << "return _iLen;" << endl;
			indent_down();
			indent(out) << "} else {" << endl;
			indent_up();
				indent(out) << "if(_oStatus)  (*_oStatus) |= _EDF_E_CRT; " << endl;
				indent(out) << "return -1;" << endl;
			indent_down();
			indent(out) << "}" << endl;
		indent_down();
		indent(out) << "} else {" << endl;
		indent_up();
			indent(out) << "if(_oStatus)  (*_oStatus) |= _EDF_E_UNK; " << endl;
			indent(out) << "return " << _dec_unknown <<"(_oPtr, _oLen);" << endl;
		indent_down();
		indent(out) << "}" << endl;
		
	indent_down();
	out <<	"}" 
		<<	endl 
		<<	endl;
}

/*------------------------------------------------------------------------------------------------------------
** Generate the Method Union AT
*/
void	Edf_GenerC::gen_setAt_Pack(ofstream& out, Edf_Type * tt, Edf_Field * tf, bool unk)
{
	ostringstream nss;
	nss 	<<	tf->get_fid()/EDF_FID_FACTOR;
	string nFid = nss.str();	
	string tn, fn, tfn; 
	fn		= tf->get_name();	
	tfn 	= get_typename(get_true_type(tf->get_type()));
	if(unk == false){
		string vFid, pFid, aFid;
		ostringstream ass;
		ass	 << "_xPtr->_presence." << fn << "[_idx/" << EDF_FID_FACTOR << "]";
		aFid =  ass.str();

		tn   = tt->get_name();
		vFid = "_xPtr->" + fn + "[_idx]";
		pFid = "_xPtr->_presence._fid[" + nFid  +  "]";

		out << "static _sInt32 _xSetAt_"  << tn << "_"	<< fn	<< "(" << tn << " * _xPtr,  _uInt32 _idx){"	<<	endl;
		indent_up();
		indent(out) << "if(_idx < " << tf->get_repeat() << "){"	<< endl;
		indent_up();
			if(get_type_id(get_true_type(tt)) == EDF_T_UNION){
				indent(out) << "_xClean_" << tn << "(_xPtr);" 	<< endl;
			}
			indent(out) << pFid << " |= (0X01 << "	<< tf->get_fid()%EDF_FID_FACTOR << ");" 	<< endl;
			indent(out) << aFid << " |= (0X01 <<"	<< "(_idx%" << EDF_FID_FACTOR << "));"		<< endl;
			indent(out) << "return (0);" 	<< endl;
	}
	else{
		CLUE_PREFIX();
		tn 	 = "void ";
		out << "static _sInt32 _xSetAt_"  << prefix<< "_"<<  fn	<< "(" << tn << " * _xPtr,  _uInt32 _idx){"	<<	endl;
		indent_up();
		CLUE_OFF_IDX_P();		CLUE_OFF_IDX_A();
		indent(out) << "if(_idx < " << tf->get_repeat() << "){"	<< endl;
		indent_up();
			if(get_type_id(get_true_type(tt)) == EDF_T_UNION){
				indent(out) << "_xClean_" << prefix<< "(_xPtr);"<< endl;
			}
			indent(out) << ELEM_P() << " |= (0X01 << "	<< tf->get_fid()%EDF_FID_FACTOR << ");" << endl;
			indent(out) << ELEM_A()<< " |= (0X01 <<"	<< "(_idx%" << EDF_FID_FACTOR << "));"		<< endl;
			indent(out) << "return (0);" 	<< endl;
	}
	indent_down();
	indent(out) << "}"				<< endl;
	indent(out) << "return (-1);" 	<< endl;
	indent_down();
	out <<	"}" 
		<<	endl 
		<<	endl;
}


void	Edf_GenerC::gen_getAt_Pack(ofstream& out, Edf_Type * tt, Edf_Field * tf, bool unk)
{
	ostringstream nss;
	nss 	<<	tf->get_fid()/EDF_FID_FACTOR;
	string nFid = nss.str();	
	string tn, fn, tfn; 
	bool 	ifn;
	
	fn		= tf->get_name();	
	ifn 	= true;
	tfn 	= get_typename(get_true_type(tf->get_type()));
	if(tfn == ""){
		tfn = "void ";
		ifn = false;
	}
	
	if(unk == false){
		string vFid, pFid, aFid;
		ostringstream ass;
		ass  << "_xPtr->_presence." << fn << "[_idx/" << EDF_FID_FACTOR << "]";
		aFid =	ass.str();

		tn	 = tt->get_name();
		vFid = "_xPtr->" + fn + "[_idx]";
		pFid = "_xPtr->_presence._fid[" + nFid	+  "]";

		out << "static " << tfn << "*_xGetAt_"  << tn << "_"	<< fn	<< "(" 	<< tn << " * _xPtr,	_uInt32 _idx){" <<	endl;
		indent_up();
			indent(out) << "if(_idx < " << tf->get_repeat() << "){"		<< endl;
			indent_up();
			
				indent(out) << "if((" << aFid << " & (0X01 << (_idx%" << EDF_FID_FACTOR << "))) == 0){"	<< endl;
				indent_up();
					if(ifn == true){
						indent(out) << "_xInit_" << tfn << "(&(" << vFid << "));" << endl;
					}
					else{
						indent(out) << "_xInit_" << tn	<< "_" << fn << "(&(" << vFid << "));" << endl;
					}
				indent_down();
				indent(out) << "}" << endl;

				indent(out) << "return &(" << vFid << ");"					<< endl;
	}
	else{
		CLUE_PREFIX();
		tn	 = "void ";
		out << "static " << tfn << "*_xGetAt_"  << prefix<< "_"	<<	fn	<< "(" 	<< tn << " * _xPtr,	_uInt32 _idx){" <<	endl;
		indent_up();
			CLUE_OFF_IDX_V(); CLUE_OFF_IDX_A();
			indent(out) << "if(_idx < " << tf->get_repeat() << "){"		<< endl;
			indent_up();

				indent(out) << "if((" << ELEM_A() << " & (0X01 << (_idx%" << EDF_FID_FACTOR << "))) == 0){" << endl;
				indent_up();
					if(ifn == true){
						indent(out) << "_xInit_" << tfn << "(" << ELEM_VPTR() << ");" << endl;
					}
					else{
						indent(out) << "_xInit_" << prefix  << "_" << fn << "(" << ELEM_VPTR() << ");" << endl;
					}
				indent_down();
				indent(out) << "}" << endl;

				indent(out) << "return " << ELEM_VPTR() << ";"				<< endl;
	}
	indent_down();
	indent(out) << "}" 				<< endl;
	indent(out) << "return (NULL);" << endl;
	indent_down();
	out <<	"}" 	
		<<	endl 
		<<	endl;	
}

void	Edf_GenerC::gen_decAt_Pack(ofstream& out, Edf_Type * tt, Edf_Field * tf, bool unk)
{
	string sComment;
	ostringstream nss;
	nss 	<<	tf->get_fid()/EDF_FID_FACTOR;
	string 	nFid = nss.str();	

	string tn, fn, tfn, pFid, aFid; 
	Edf_Type *	ttf;
	
	ttf 	= get_true_type(tf->get_type());
	fn		= tf->get_name();	
	tfn 	= get_typename(get_true_type(tf->get_type()));
	if(tfn == ""){
		tfn = "void ";
	}
	if(unk == false){
		ostringstream ass; 
		string vFid;
		tn	 = tt->get_name();
		ass  << "_xPtr->_presence." << fn;
		vFid = "_xPtr->" + fn;
		aFid =	ass.str();
		pFid = "_xPtr->_presence._fid[" + nFid	+  "]";

		out << "static _sInt32 _xDecAt_"<< tn	<< "_"	<< fn	<< "(" << tn	<< " * _xPtr, _uInt8 * _oPtr, _sInt32 _oLen, _sInt32 * _oStatus){" <<	endl;
		indent_up();
			indent(out) << "_uInt32 _jdx, _idx, _iCnt, _mCnt, _mask, _sum;"	<< endl;
			indent(out) << "_uInt8  *_vPtr, *_sPtr, *_iPtr;" 	<< endl;
			indent(out) << "_uInt32 *_lPtr, *_hPtr, *_aPtr;"	<< endl;
			if(tfn != "void "){
				indent(out) << "_sInt32 (* _fDecode)() = (_sInt32 (*)())_xDecode_" 	<< tfn << ";"				<< endl;
			}else{
				indent(out) << "_sInt32 (* _fDecode)() = (_sInt32 (*)())_xDecode_"  << tn << "_"<< fn 	<< ";"	<< endl;
			}
			indent(out) << "_uInt32 _sOff = (_uInt32)sizeof(" << vFid << "[0]);"	<< endl;

			indent(out) << "_sPtr = (_uInt8 *)(" << vFid <<");" << endl; 
	}
	else{
		CLUE_PREFIX();
		tn	 = "void ";

		out << "static _sInt32 _xDecAt_"<< prefix<< "_"<< fn	<< "(" << tn	<< " * _xPtr, _uInt8 * _oPtr, _sInt32 _oLen, _sInt32 * _oStatus){"	<<	endl;
		indent_up();
			indent(out) << "_uInt32 _jdx, _idx, _iCnt, _mCnt, _mask, _sum;"	<< endl;
			indent(out) << "_uInt8  *_vPtr, *_sPtr, *_iPtr;" 	<< endl;
			indent(out) << "_uInt32 *_lPtr, *_hPtr, *_aPtr;"	<< endl;
			if(tfn != "void "){
				indent(out) << "_sInt32 (* _fDecode)() = (_sInt32 (*)())_xDecode_" 	<< tfn << ";"				<< endl;
			}else{
				indent(out) << "_sInt32 (* _fDecode)() = (_sInt32 (*)())_xDecode_" << prefix<< "_"<< fn << ";"	<< endl;
			}
			CLUE_OFF_IDX_P(); CLUE_OFF_IDX_S();			CLUE_OFF_IDX_VBEG();	 			CLUE_OFF_IDX_ABEG();
			pFid = ELEM_P(); aFid = ELEM_APTR();
			
			indent(out) << "_sPtr = (_uInt8 *)("	<<	ELEM_VPTR() <<");"				<< endl;
	}

			// Add the Comment 
			sComment = "Check Type : ";
			sComment += tf->get_name();
			gen_comment(out, sComment);

			indent(out) << "_mask = *(_oPtr + 1);" 			<< endl;
			indent(out) << "if((_mask >> 4) == "	<< EDF_T_PACK << "){" << endl;
			indent_up();
				/* Judge the Count */
				_ENDVAL_NTOHL("*((_uInt32 *)(_oPtr + 2))");
				_ENDVAL_NTOHL("*((_uInt32 *)(_oPtr + 6))");
				_ENDVAL_NTOHL("*((_uInt32 *)(_oPtr + 10))");
				indent(out) << "_sum  = *((_uInt32*)(_oPtr + 2));" 			<< endl;
				indent(out) << "_iCnt = *((_uInt32*)(_oPtr + 6));" 			<< endl;
				indent(out) << "_mCnt = *((_uInt32*)(_oPtr + 10));" 		<< endl;
				indent(out) << "_lPtr = (_uInt32 *)(_oPtr + _sum - (_iCnt*4) - (_mCnt*8));" << endl;
				indent(out) << "_hPtr = (_uInt32 *)(_lPtr + _iCnt);" << endl;

				indent(out) << "if((_iCnt > 0) && (_iCnt <= " << tf->get_repeat() << ")){" << endl;
				indent_up();

					// Add the Comment 
					sComment = "Presence Of A: ";
					sComment += tf->get_name();
					gen_comment(out, sComment);					
					indent(out) << "_aPtr = "	<< aFid <<";"			<< endl; 

					indent(out) << "for(_jdx = 0, _sum = 14; _jdx < _mCnt; _jdx++, _hPtr+=2){" << endl;
					indent_up();
						_ENDVAL_NTOHL("*(_hPtr)");
						_ENDVAL_NTOHL("*(_hPtr + 1)");
						indent(out) << "_mask = *(_hPtr + 1);  _aPtr[*_hPtr] = _mask; _vPtr = _sPtr + ((*_hPtr) * 32 * _sOff);" << endl;
						indent(out) << "for(_idx = 0; _idx < 32; _idx++, _mask >>= 1){"	<< endl;
						indent_up();
							indent(out) << "if(_mask == 0){" << endl;
							indent_up();
								indent(out) << "break;" << endl;
							indent_down();
							indent(out) << "}" << endl;
							indent(out) << "if((_mask & 0X01) && (_iCnt > 0)){"				<< endl; 
							indent_up();
								indent(out) << " _iPtr = (_vPtr + _idx * _sOff); " 			<< endl;
								_ENDVAL_NTOHL("*(_lPtr)");
								indent(out) << "if(_fDecode(_iPtr, _oPtr + _sum, (_sInt32)((*_lPtr) - _sum), _oStatus) < 0){"			<< endl;
								indent_up();
										indent(out) << "if(_oStatus)  (*_oStatus) |= (_EDF_E_CRT); " << endl;
										indent(out) << "return -1;" 	<< endl;
								indent_down();
								indent(out) << "}"	<< endl;
								indent(out) << "_sum = *_lPtr; 	_lPtr += 1; _iCnt -=1; " 	<< endl;
							indent_down();
							indent(out) << "}"			<< endl;
						indent_down();
						indent(out) << "}"			<< endl;
					indent_down();
					indent(out) << "}"			<< endl;

					indent(out) << pFid << " |= (0X01 << " << tf->get_fid()%EDF_FID_FACTOR << ");" 					<< endl;
					indent(out) << "return *((_sInt32 *)(_oPtr + 2));" << endl;
    
				indent_down();
				indent(out) << "} else {" << endl;
				indent_up();
					// Add the Comment 
					sComment = "Check OOR : ";
					sComment += tf->get_name();
					gen_comment(out, sComment);

					indent(out) << "if(_oStatus)  (*_oStatus) |= (_EDF_E_CRT|_EDF_E_OOR); " << endl;
					indent(out) << "return -1;" << endl;
				indent_down();
				indent(out) << "}" << endl;
					
			indent_down();
			indent(out) << "} else {"		<< endl;
			indent_up();
				indent(out) << "if(_oStatus)  (*_oStatus) |= _EDF_E_UNK; " << endl;
				indent(out) << "return -1;" << endl;
			indent_down();
			indent(out) << "}"				<< endl;

		indent(out) << "return (-1);"		<< endl;
		indent_down();	
		out <<	"}"		
			<<	endl	
			<<	endl;	
}

void	Edf_GenerC::gen_encAt_Pack(ofstream& out, Edf_Type * tt, Edf_Field * tf, bool unk)
{
	string sComment;
	ostringstream nss;	
	string tn, fn, tfn; 
	Edf_Type *	ttf;
	ttf 	= get_true_type(tf->get_type());
	fn		= tf->get_name();	
	tfn 	= get_typename(get_true_type(tf->get_type()));
	if(tfn == ""){
		tfn = "void ";
	}

	
	int  repeat = (tf->get_repeat() + EDF_FID_FACTOR - 1)/EDF_FID_FACTOR;
	
	if(unk == false){
		ostringstream ass; 
		string vFid, aFid;
		tn	 = tt->get_name();
		ass  << "_xPtr->_presence." << fn;
		vFid = "_xPtr->" + fn;
		aFid =	ass.str();

		out << "static _sInt32 _xEncAt_"<< tn	<< "_"	
			<< fn	<< "(" << tn	<< " * _xPtr, _uInt8 * _oPtr, _sInt32 _oLen, _sInt32 * _oStatus){" <<	endl;
		indent_up();
			indent(out) << "_sInt32 _idx, _jdx, _iCnt, _mCnt, _mask, _len;"	<< endl;
			indent(out) << "_uInt8  *_vPtr, *_iPtr, *_sPtr;" 			<< endl;
			indent(out) << "_uInt32 *_hPtr, *_aPtr;"					<< endl;
			indent(out) << "_uInt32 _aTmp[" 	<<  repeat*2				<< "];" << endl;
			indent(out) << "_uInt32 _lTmp[" 	<<  tf->get_repeat()	<< "];" << endl;

			if(tfn != "void "){
				indent(out) << "_sInt32 (* _fEncode)() = (_sInt32 (*)())_xEncode_" 	<< tfn << ";"				<< endl;
			}else{
				indent(out) << "_sInt32 (* _fEncode)() = (_sInt32 (*)())_xEncode_" 	<< tn	<< "_"<< fn << ";"	<< endl;
			}
			indent(out) << "_uInt32 _sOff = (_uInt32)sizeof(" << vFid << "[0]);"	<< endl;
			indent(out) << "_sPtr = (_uInt8 *)(" << vFid <<");" 					<< endl; 
			indent(out) << "_aPtr = "	<< aFid <<";"								<< endl; 
	}
	else{
		CLUE_PREFIX();
		tn	 = "void ";
		out << "static _sInt32 _xEncAt_"	<< prefix	 	<< "_"
			<< fn	<< "(" << tn	<< " * _xPtr, _uInt8 * _oPtr, _sInt32 _oLen, _sInt32 * _oStatus){"	<<	endl;
		indent_up();
			indent(out) << "_sInt32 _idx, _jdx, _iCnt, _mCnt, _mask, _len;"	<< endl;
			indent(out) << "_uInt8	*_vPtr, *_iPtr, *_sPtr;"			<< endl;
			indent(out) << "_uInt32 *_hPtr, *_aPtr;"					<< endl;
			indent(out) << "_uInt32 _aTmp[" 	<<  repeat*2				<< "];" << endl;
			indent(out) << "_uInt32 _lTmp[" 	<<  tf->get_repeat()	<< "];" << endl;
			
			if(tfn != "void "){
				indent(out) << "_sInt32 (* _fEncode)() = (_sInt32 (*)())_xEncode_" 	<< tfn << ";"					<< endl;
			}else{
				indent(out) << "_sInt32 (* _fEncode)() = (_sInt32 (*)())_xEncode_" 	<< prefix<< "_"	<< fn << ";"	<< endl;
			}
			CLUE_OFF_IDX_S();			CLUE_OFF_IDX_VBEG();	 			CLUE_OFF_IDX_ABEG();
			indent(out) << "_sPtr = (_uInt8 *)("	<<	ELEM_VPTR() <<");"				<< endl;
			indent(out) << "_aPtr = "				<<	ELEM_APTR() <<";"					<< endl;
	}
		indent(out) << "if( EDF_PACK_MIN <= _oLen){"		<< endl;
		indent_up();
			indent(out) << "*_oPtr = "			<< (tf->get_fid()<< 0X01)	<< " | (0X01);" << endl; 
			indent(out) << "*(_oPtr + 1) = "	<< (EDF_T_PACK << 0X04)<< ";"	<< endl; 
			indent(out) << "_hPtr = _aTmp;"		<< endl; 
			indent(out) << "_iPtr = (_uInt8 *) (_oPtr + 14);" 	<< endl;
			indent(out) << "_oLen  -= 14;"		<< endl;

			indent(out) << "for(_jdx = 0, _iCnt = 0, _mCnt = 0; _jdx < " << repeat << "; _jdx++, _aPtr++, _sPtr+=(32 * _sOff)){" << endl;
			indent_up();
				indent(out) << "if((*_aPtr) != 0){" << endl;
				indent_up();
					indent(out) << "*_hPtr = _jdx; *(_hPtr + 1) = *_aPtr;" 	<< endl;
					_ENDVAL_HTONL("*_hPtr");
					_ENDVAL_HTONL("*(_hPtr + 1)");
					indent(out) << "_hPtr += 2; _mCnt += 1; _oLen -= 8;"	<< endl;
					
					indent(out) << "for(_idx = 0, _mask =(*_aPtr); _idx < 32; _idx++, _mask >>= 1){"	<< endl;
					indent_up();
						indent(out) << "if(_mask == 0){" << endl;
						indent_up();
							indent(out) << "break;" << endl;
						indent_down();
						indent(out) << "}" << endl;

						indent(out) << "if(_mask & 0X01){"	<< endl; 
						indent_up();
							indent(out) << "_vPtr = _sPtr + (_idx * _sOff);" 		<< endl;
							indent(out) << "_len  = _fEncode(_vPtr, _iPtr, _oLen, _oStatus);" << endl;
							
							indent(out)	<< "if(_len > 0){"	<< endl; 
							indent_up();
								indent(out) << "_iPtr += _len; 	_oLen -= _len;"	<< endl;
								indent(out) << "_lTmp[_iCnt] = (_uInt32)(_iPtr - _oPtr);"		<< endl;
								_ENDVAL_HTONL("_lTmp[_iCnt]");
								indent(out) << "_iCnt += 1;" 				<< endl;
							indent_down();
							indent(out) << "} else {"		<< endl;
							indent_up();
								// Add the Comment 
								sComment = "Check OOM : ";
								sComment += tf->get_name();
								gen_comment(out, sComment);
							
								indent(out) << "if(_oStatus)  (*_oStatus) |= _EDF_E_OOM; " << endl;
								indent(out) << "return 0;" << endl;
							indent_down();
							indent(out) << "}"	<< endl;
						indent_down();
						indent(out) << "}" 	<< endl;
					indent_down();
					indent(out) << "}"		<< endl;
				indent_down();
				indent(out) << "}"		<< endl;
			indent_down();
			indent(out) << "}"		<< endl;

			indent(out) << "if( _iCnt > 0){" << endl;
			indent_up();
				indent(out) << "_hPtr = (_uInt32 *)_iPtr;"	<< endl; 
				indent(out) << "_xEdfCopy(_hPtr, _lTmp, _iCnt * 4); _hPtr += _iCnt;"		<< endl;
				indent(out) << "_xEdfCopy(_hPtr, _aTmp, _mCnt * 8); _hPtr += (_mCnt * 2);"	<< endl;
				indent(out) << "_jdx  = (_uInt32)((_uInt8*)_hPtr - (_uInt8*)_oPtr);" 		<< endl;

				indent(out) << "*((_uInt32 *)(_oPtr + 2))  = _jdx;" << endl;
				indent(out) << "*((_uInt32 *)(_oPtr + 6))  = _iCnt;" << endl;
				indent(out) << "*((_uInt32 *)(_oPtr + 10)) = _mCnt;" << endl;
				
				_ENDVAL_HTONL("*((_uInt32 *)(_oPtr + 2))");
				_ENDVAL_HTONL("*((_uInt32 *)(_oPtr + 6))");
				_ENDVAL_HTONL("*((_uInt32 *)(_oPtr + 10))");
				indent(out) << "return _jdx;" << endl;
			indent_down();
			indent(out) << "}"		<< endl;

		indent_down();
		indent(out) << "}"			<< endl;
		/* OOM */
		indent(out) << "if(_oStatus)  (*_oStatus) |= _EDF_E_OOM; " << endl;
		indent(out) << "return 0;" << endl;
	indent_down();	
	out <<	"}"		
		<<	endl	
		<<	endl;	
}

void	Edf_GenerC::gen_sizeAt_Pack(ofstream& out, Edf_Type * tt, Edf_Field * tf, bool unk)
{
	ostringstream nss;	
	string tn, fn, tfn; 
	Edf_Type *	ttf;
	ttf 	= get_true_type(tf->get_type());
	fn		= tf->get_name();	
	tfn 	= get_typename(get_true_type(tf->get_type()));
	if(tfn == ""){
		tfn = "void ";
	}
	if(unk == false){
		ostringstream ass; 
		string vFid, aFid;
		tn	 = tt->get_name();
		ass  << "_xPtr->_presence." << fn;
		vFid = "_xPtr->" + fn;
		aFid =	ass.str();

		out << "static _sInt32 _xSizeAt_"<< tn	<< "_"	
			<< fn	<< "(" << tn	<< " * _xPtr){" <<	endl;
		indent_up();
			indent(out) << "_sInt32 _idx, _jdx, _mCnt, _mask, _sum;"	<< endl;
			indent(out) << "_uInt8  *_vPtr, *_iPtr;" 	<< endl;
			indent(out) << "_uInt32 *_aPtr;"			<< endl;
			if(tfn != "void "){
				indent(out) << "_sInt32 (* _fSizeOf)() = (_sInt32 (*)())_xSizeOf_" 	<< tfn << ";"				<< endl;
			}else{
				indent(out) << "_sInt32 (* _fSizeOf)() = (_sInt32 (*)())_xSizeOf_" 	<< tn	<< "_"<< fn << ";"	<< endl;
			}
			indent(out) << "_uInt32 _sOff = (_uInt32)sizeof(" << vFid << "[0]);"	<< endl;
			indent(out) << "_vPtr = (_uInt8 *)(" << vFid <<");" 					<< endl; 
			indent(out) << "_aPtr = "	<< aFid <<";"								<< endl; 
	}
	else{
		CLUE_PREFIX();
		tn	 = "void ";
		out << "static _sInt32 _xSizeAt_"	<< prefix	 	<< "_"
			<< fn	<< "(" << tn	<< " * _xPtr){"	<<	endl;
		indent_up();
			indent(out) << "_sInt32 _idx, _jdx, _mCnt, _mask, _sum;"	<< endl;
			indent(out) << "_uInt8  *_vPtr, *_iPtr;" 	<< endl;
			indent(out) << "_uInt32 *_aPtr;"			<< endl;
			if(tfn != "void "){
				indent(out) << "_sInt32 (* _fSizeOf)() = (_sInt32 (*)())_xSizeOf_" 	<< tfn << ";"					<< endl;
			}else{
				indent(out) << "_sInt32 (* _fSizeOf)() = (_sInt32 (*)())_xSizeOf_" 	<< prefix<< "_"	<< fn << ";"	<< endl;
			}
			CLUE_OFF_IDX_S();			CLUE_OFF_IDX_VBEG();	 			CLUE_OFF_IDX_ABEG();
			indent(out) << "_vPtr = (_uInt8 *)("	<<	ELEM_VPTR() <<");"				<< endl;
			indent(out) << "_aPtr = "				<<	ELEM_APTR() <<";"					<< endl;
	}
			int  repeat = (tf->get_repeat() + EDF_FID_FACTOR - 1)/EDF_FID_FACTOR;
			indent(out) << "for(_jdx = 0, _mCnt = 0, _sum = 0; _jdx < " << repeat << "; _jdx++, _aPtr++, _vPtr+=(32 * _sOff)){" << endl;
			indent_up();
				indent(out) << "if((*_aPtr) != 0){" << endl;
				indent_up();
					indent(out) << "_mCnt += 1;" 	<< endl;
					indent(out) << "for(_idx = 0, _mask = (*_aPtr); _idx < 32; _idx++, _mask >>= 1){" << endl;
					indent_up();
						indent(out) << "if(_mask == 0){" << endl;
						indent_up();
							indent(out) << "break;" << endl;
						indent_down();
						indent(out) << "}" << endl;
						indent(out) << "if(_mask & 0X01){"	<< endl; 
						indent_up();
							indent(out) << "_iPtr = _vPtr + (_idx * _sOff);"	<< endl;
							indent(out) << "_sum += _fSizeOf(_iPtr) + 4;" 		<< endl;
						indent_down();
						indent(out) << "}"	<< endl;
					indent_down();
					indent(out) << "}"		<< endl;
				indent_down();
				indent(out) << "}"		<< endl;
			indent_down();
			indent(out) << "}"		<< endl;

			indent(out) << "if( _sum > 0){" << endl;
			indent_up();
				indent(out) << "return (_sum + 14 + (_mCnt * 8));" 	<< endl;
			indent_down();
			indent(out) << "}"			<< endl;

		indent(out) << "return (0);"	<< endl;
		indent_down();	
		out <<	"}"		
			<<	endl	
			<<	endl;	
}

void	Edf_GenerC::gen_count_Pack(ofstream& out, Edf_Type * tt, Edf_Field * tf, bool unk)
{
	ostringstream nss;	
	string tn, fn, tfn; 
	Edf_Type *	ttf;
	ttf 	= get_true_type(tf->get_type());
	fn		= tf->get_name();	
	tfn 	= get_typename(get_true_type(tf->get_type()));
	if(tfn == ""){
		tfn = "void ";
	}
	if(unk == false){
		ostringstream ass; 
		string vFid, aFid;
		tn	 = tt->get_name();
		ass  << "_xPtr->_presence." << fn;
		vFid = "_xPtr->" + fn;
		aFid =	ass.str();

		out << "static _sInt32 _xCount_"<< tn	<< "_"	
			<< fn	<< "(" << tn	<< " * _xPtr){" <<	endl;
		indent_up();
			indent(out) << "_sInt32 _idx, _jdx, _cnt;"	<< endl;
			indent(out) << "_uInt32 _mask, *_aPtr;"	<< endl << endl;
			indent(out) << "_aPtr = "	<< aFid <<";"								<< endl; 
	}
	else{
		CLUE_PREFIX();
		tn	 = "void ";
		out << "static _sInt32 _xCount_"	<< prefix	 	<< "_"
			<< fn	<< "(" << tn	<< " * _xPtr){"	<<	endl;
		indent_up();
			indent(out) << "_sInt32 _idx, _jdx, _cnt;"		<< endl;
			indent(out) << "_uInt32 _mask, *_aPtr;"	<< endl << endl;
			CLUE_OFF_IDX_ABEG();
			indent(out) << "_aPtr = "				<<	ELEM_APTR() <<";"					<< endl;
	}
			int  repeat = (tf->get_repeat() + EDF_FID_FACTOR - 1)/EDF_FID_FACTOR;
			indent(out) << "for(_jdx = 0, _cnt = 0; _jdx < " << repeat << "; _jdx++, _aPtr++){" << endl;
			indent_up();
				indent(out) << "if((*_aPtr) != 0){" << endl;
				indent_up();
					indent(out) << "for(_idx = 0, _mask = (*_aPtr); _idx < 32; _idx++, _mask >>= 1){" << endl;
					indent_up();
						indent(out) << "if(_mask == 0){" << endl;
						indent_up();
							indent(out) << "break;" << endl;
						indent_down();
						indent(out) << "}" << endl;
						indent(out) << "if(_mask & 0X01){"	<< endl; 
						indent_up();
							indent(out) << "_cnt += 1;" 						<< endl;
						indent_down();
						indent(out) << "}"	<< endl;
					indent_down();
					indent(out) << "}"		<< endl;
				indent_down();
				indent(out) << "}"		<< endl;
			indent_down();
			indent(out) << "}"		<< endl;

		indent(out) << "return _cnt;" << endl;
	indent_down();	
	out <<	"}"		
		<<	endl	
		<<	endl;	
}

void	Edf_GenerC::gen_methodAt_Pack(ofstream& out, Edf_Type * tt, Edf_Field * tf, bool unk)
{
	// Add the Comment 
	string sComment = "Methods of Pack Field[]: ";
	sComment += tf->get_name();
	gen_comment(out, sComment);

	gen_existAt_Pack(out, tt, tf, unk);
	gen_clearAt_Pack(out, tt, tf, unk);
	
	gen_getAt_Pack(out, tt, tf, unk);
	gen_setAt_Pack(out, tt, tf, unk);

	gen_encAt_Pack(out, tt, tf, unk);
	gen_sizeAt_Pack(out, tt, tf, unk);
	gen_count_Pack(out, tt, tf, unk);
	gen_decAt_Pack(out, tt, tf, unk);
}


/*------------------------------------------------------------------------------------------------------------
** Generate the Method Simple 
** Integer / Double
*/
void	Edf_GenerC::gen_set_simple(ofstream& out, Edf_Type * tt, Edf_Field * tf, bool unk)
{
	ostringstream nss;
	nss		<<  tf->get_fid()/EDF_FID_FACTOR;
	string nFid	= nss.str();	
	string tn, fn, tfn;	
	Edf_Type * ttf;

	fn 		= tf->get_name();	
	ttf 	= get_true_type(tf->get_type());
	tfn 	= get_typename(ttf);
	if(unk == false){
		string vFid, pFid;
		tn   = tt->get_name();
		vFid = "_xPtr->" + fn;
		pFid = "_xPtr->_presence._fid[" + nFid  +  "]";
		out << "static _sInt32 _xSet_"	<< tn << "_" << fn	<< "(" << tn << " * _xPtr, " << tfn << " _xVal){" <<	endl;
		indent_up();
		#if 1
		if(get_type_id(get_true_type(tt)) == EDF_T_UNION){
			indent(out) << "_xClean_" << tn << "(_xPtr);" << endl;
		}
		#endif
		indent(out) << vFid << " = _xVal"<< ";"											<< endl;
		indent(out) << pFid << " |= (0X01 << " << tf->get_fid()%EDF_FID_FACTOR << ");" 	<< endl;
		indent(out) << "return (0);" 		<< endl;
	}
	else{
		CLUE_PREFIX();
		tn 	 = "void ";
		out << "static _sInt32 _xSet_"	<< prefix << "_"<< fn<< "(" << tn 	<< " * _xPtr, " << tfn << " _xVal){"	<<endl;
		indent_up();
		CLUE_OFF_V();		CLUE_OFF_P();
		#if 1
		if(get_type_id(get_true_type(tt)) == EDF_T_UNION){
			indent(out) << "_xClean_" << prefix << "(_xPtr);" 									<< endl;
		}
		#endif
		indent(out) << ELEM_V() 		<< " = _xVal"<< ";"											<< endl;
		indent(out) << ELEM_P() 		<< " |= (0X01 << " << tf->get_fid()%EDF_FID_FACTOR << ");" 	<< endl;
		indent(out) << "return (0);" 		<< endl;
	}
	indent_down();
	out <<	"}" <<	endl <<	endl;
}

void	Edf_GenerC::gen_exist_simple(ofstream& out, Edf_Type * tt, Edf_Field * tf, bool unk)
{
	ostringstream nss;
	nss 	<<	tf->get_fid()/EDF_FID_FACTOR;
	string nFid = nss.str();	
	string tn, fn, tfn; 
	fn		= tf->get_name();	
	tfn 	= get_typename(get_true_type(tf->get_type()));
	if(unk == false){
		string pFid;
		tn	 = tt->get_name();
		pFid = "_xPtr->_presence._fid[" + nFid	+  "]";
		out << "static _sInt32 _xExist_" << tn	<< "_"	<< fn	<< "(" << tn	<< " * _xPtr){"		<<	endl;
		indent_up();
		indent(out) << "if(" << pFid <<  " & (0X01 << " << tf->get_fid()%EDF_FID_FACTOR << ")){" 	<< endl;
	}
	else{
		CLUE_PREFIX();
		tn	 = "void ";
		out << "static _sInt32 _xExist_" << prefix	<< "_"	<< fn	<< "(" << tn<< " * _xPtr){"		<<	endl;
		indent_up();
		CLUE_OFF_P();
		indent(out) << "if(" << ELEM_P() <<  " & (0X01 << " << tf->get_fid()%EDF_FID_FACTOR << ")){" << endl;
	}
		indent_up();
		indent(out) << "return (1);"	<< endl;
		indent_down();
		indent(out) << "}" 				<< endl;
	indent(out) << "return (0);" 		<< endl;
	indent_down();
	out <<	"}" 	<<	endl 			<<	endl;
}

void	Edf_GenerC::gen_clear_simple(ofstream& out, Edf_Type * tt, Edf_Field * tf, bool unk)
{
	ostringstream nss;
	nss 	<<	tf->get_fid()/EDF_FID_FACTOR;
	string nFid = nss.str();	
	string tn, fn, tfn; 
	fn		= tf->get_name();	
	tfn 	= get_typename(get_true_type(tf->get_type()));
	if(unk == false){
		string pFid;
		tn	 = tt->get_name();
		pFid = "_xPtr->_presence._fid[" + nFid	+  "]";
		out << "static void _xClear_" << tn	<< "_"	<< fn	<< "(" << tn	<< " * _xPtr){"		<<	endl;
		indent_up();
		indent(out) << pFid <<  " &= ~(0X01 << " << tf->get_fid()%EDF_FID_FACTOR << ");" 	<< endl;
	}
	else{
		CLUE_PREFIX();
		tn	 = "void ";
		out << "static void _xClear_" << prefix	<< "_"	<< fn	<< "(" << tn<< " * _xPtr){"		<<	endl;
		indent_up();
		CLUE_OFF_P();
		indent(out) << ELEM_P() <<  " &= ~(0X01 << " << tf->get_fid()%EDF_FID_FACTOR << ");" << endl;
	}
	indent(out) << "return;" 		<< endl;
	indent_down();
	out <<	"}" 	<<	endl 		<<	endl;
}

void	Edf_GenerC::gen_get_simple(ofstream& out, Edf_Type * tt, Edf_Field * tf, bool unk)
{
	ostringstream nss;
	nss 	<<	tf->get_fid()/EDF_FID_FACTOR;
	string nFid = nss.str();	
	string tn, fn, tfn; 
	Edf_Type * ttf;

	fn		= tf->get_name();	
	ttf 	= get_true_type(tf->get_type());
	tfn 	= get_typename(ttf);
	if(unk == false){
		string vFid, pFid;
		tn	 = tt->get_name();
		vFid = "_xPtr->" + fn;
		pFid = "_xPtr->_presence._fid[" + nFid	+  "]";
		out << "static "  	<< tfn 	<< " _xGet_" << tn	<< "_"	<< fn	<< "(" 	<< tn	<< " * _xPtr ){"	<<	endl;
		indent_up();
		indent(out) << tfn << " _xVal;" << endl;
		indent(out) << "_xVal = (" << tfn <<")(" << vFid << ");"	<< endl;
		indent(out) << "return _xVal;"	<< endl;
	}
	else{
		CLUE_PREFIX();
		tn	 = "void ";
		out << "static "	<<	tfn << " _xGet_" << prefix<< "_"	<< fn	<< "(" 	<< tn	<< " * _xPtr ){"	<<	endl;
		indent_up();
		indent(out) << tfn << " _xVal;" << endl;
		CLUE_OFF_V();
		indent(out) << "_xVal = " << ELEM_V() << ";"		<< endl;
		indent(out) << "return _xVal;"	<< endl;
	}
	indent_down();	
	out <<	"}" 	<<	endl 	<<	endl;	
}

void	Edf_GenerC::gen_enc_simple(ofstream& out, Edf_Type * tt, Edf_Field * tf, bool unk)
{
	ostringstream nss;
	nss 	<<	tf->get_fid()/EDF_FID_FACTOR;
	string 	nFid = nss.str();	
	string 	tn, fn, tfn; 	
	Edf_Type * 	ttf;
	
	ttf 	= get_true_type(tf->get_type());
	fn		= tf->get_name();	
	tfn 	= get_typename(get_true_type(tf->get_type()));
	if(unk == false){
		string vFid;
		tn	 = tt->get_name();
		vFid = "_xPtr->" + fn;
		out << "static _sInt32 _xEnc_"	<< tn	<< "_"	<< fn	<< "(" << tn 	<< " * _xPtr, _uInt8 * _oPtr, _sInt32 _oLen, _sInt32 * _oStatus){"	<<	endl;
 			indent_up();
			/* Add the Val Define */
			indent(out) << tfn << " _xVal;" << endl;
			
			indent(out) << "if((" << get_simple_len(ttf) + 2 << ") <= _oLen){" 	<< endl;
			indent_up();
				indent(out) << "*_oPtr = " 			<< (tf->get_fid()<< 0X01) 	<< ";" 			<< endl;
				indent(out) << "*(_oPtr + 1) = " 	<< (get_type_id(ttf)<< 0X04)<< ";" 			<< endl;

				indent(out) << "_xVal = " << vFid << ";" << endl;
				/* Add Endian Support */
				gen_hton(out, ttf);
				indent(out) << "*((" << tfn << "*)(_oPtr + 2)) = _xVal;" 			<< endl;
	}
	else{
		CLUE_PREFIX();
		tn	 = "void ";
		out << "static _sInt32 _xEnc_"	<< prefix<< "_"	<< fn	<< "(" << tn	<< " * _xPtr, _uInt8 * _oPtr, _sInt32 _oLen, _sInt32 * _oStatus){"	<<	endl;
		indent_up();
			/* Add the Val Define */
			indent(out) << tfn << " _xVal;" << endl;
			
			CLUE_OFF_V();
			indent(out) << "if((" << get_simple_len(ttf) + 2 << ") <= _oLen){" 	<< endl;
			indent_up();
				indent(out) << "*_oPtr = " 			<< (tf->get_fid()<< 0X01) 	<< ";" 			<< endl;
				indent(out) << "*(_oPtr + 1) = " 	<< (get_type_id(ttf)<< 0X04)<< ";" 			<< endl;

				indent(out) << "_xVal = " << ELEM_V() << ";" << endl;
				/* Add Endian Support */
				gen_hton(out, ttf);
				indent(out) << "*((" << tfn << "*)(_oPtr + 2)) = _xVal;" 			<< endl;
	}
			indent(out) << "return (" << get_simple_len(ttf) + 2 << ");"			<< endl;
		indent_down();
		indent(out) << "}" 			<< endl;
		
		// Add the Comment 
		string sComment = "Check OOM : ";
		sComment += tf->get_name();
		gen_comment(out, sComment);
		
		indent(out) << "if(_oStatus)  (*_oStatus) |= _EDF_E_OOM; " << endl;
		indent(out) << "return 0;" << endl;
	indent_down();
	out <<	"}" 	<<	endl 		<<	endl;
}

void	Edf_GenerC::gen_size_simple(ofstream& out, Edf_Type * tt, Edf_Field * tf, bool unk)
{
	ostringstream nss;
	nss 	<<	tf->get_fid()/EDF_FID_FACTOR;
	string 	nFid = nss.str();	
	string 	tn, fn, tfn; 	
	Edf_Type * 	ttf;
	
	ttf 	= get_true_type(tf->get_type());
	fn		= tf->get_name();	
	tfn 	= get_typename(get_true_type(tf->get_type()));
	if(unk == false){
		string vFid;
		tn	 = tt->get_name();
		vFid = "_xPtr->" + fn;
		out << "static _sInt32 _xSize_"	<< tn	<< "_"	
			<< fn	<< "(" << tn 	<< " * _xPtr){"	<<	endl;
		indent_up();
	}
	else{
		CLUE_PREFIX();
		tn	 = "void ";
		out << "static _sInt32 _xSize_"	<< prefix<< "_"	
			<< fn	<< "(" << tn	<< " * _xPtr){"	<<	endl;
		indent_up();
	}
			indent(out) << "return ("  << get_simple_len(ttf) + 2 << ");"			<< endl;
	indent_down();
	out <<	"}" 	<<	endl 		<<	endl;
}

void	Edf_GenerC::gen_dec_simple(ofstream& out, Edf_Type * tt, Edf_Field * tf, bool unk)
{
	ostringstream nss;
	nss 	<<	tf->get_fid()/EDF_FID_FACTOR;
	string 	nFid = nss.str();	
	
	string 	tn, fn, tfn, pFid; 	
	Edf_Type * 	ttf;
	int 		tid;
	
	ttf 	= get_true_type(tf->get_type());
	tid 	= get_type_id(ttf);
	fn		= tf->get_name();	
	tfn 	= get_typename(get_true_type(tf->get_type()));
	if(unk == false){
		string vFid;
		tn	 = tt->get_name();
		vFid = "_xPtr->" + fn;
		pFid = "_xPtr->_presence._fid[" + nFid	+  "]";
		out << "static _sInt32 _xDec_"	<< tn	<< "_"	<< fn	<< "(" << tn	<< " * _xPtr, _uInt8 * _oPtr, _sInt32 _oLen, _sInt32 * _oStatus){"	<<	endl;
		indent_up();
			indent(out) << "_uInt8 	_iType;" << endl;
			/* Add the Val Define */
			indent(out) << tfn << " _xVal;" << endl;
			#if 0
			indent(out) << "if((" << get_simple_len(ttf) + 2 << ") <= _oLen){" 	<< endl;
			indent_up();
			#endif
				indent(out) << "_iType = *(_oPtr + 1);" << endl;
				indent(out) << "if((_iType >> 4) == " 	<< tid << "){" 			<< endl;
				indent_up();
					indent(out) << "_xVal = *((" << tfn << "*)(_oPtr + 2));"	<< endl;
					/* Add Endian Support */
					gen_ntoh(out, ttf);
					indent(out) << vFid << "= _xVal;" 	<< endl;
	}
	else{
		CLUE_PREFIX();
		tn	 = "void ";
		out << "static _sInt32 _xDec_"	<< prefix<< "_"	<< fn	<< "(" << tn	<< " * _xPtr, _uInt8 * _oPtr, _sInt32 _oLen, _sInt32 * _oStatus){"	<<	endl;
		indent_up();
			indent(out) << "_uInt8	_iType;" << endl;
			/* Add the Val Define */
			indent(out) << tfn << " _xVal;" << endl;
			CLUE_OFF_V();
			CLUE_OFF_P();
			pFid = ELEM_P();
			#if 0
			indent(out) << "if((" << get_simple_len(ttf) + 2 << ") <= _oLen){" 	<< endl;
			indent_up();
			#endif
				indent(out) << "_iType = *(_oPtr + 1);" << endl;
				indent(out) << "if((_iType >> 4) == "	<< tid << "){" 				<< endl;
				indent_up();
					indent(out) << "_xVal = *((" << tfn << "*)(_oPtr + 2));"	<< endl;
					/* Add Endian Support */
					gen_ntoh(out, ttf);
					indent(out) << ELEM_V() << "= _xVal;" 	<< endl;
	}
					indent(out) << pFid << " |= (0X01 << " << tf->get_fid()%EDF_FID_FACTOR << ");"		<< endl;
					indent(out) << "return (" << get_simple_len(ttf) + 2 << ");"							<< endl;
				indent_down();
				indent(out) << "} else {" << endl;
				indent_up();
					indent(out) << "if(_oStatus)  (*_oStatus) |= _EDF_E_UNK; " << endl;
					indent(out) << "return " << _dec_unknown <<"(_oPtr, _oLen);" << endl;
				indent_down();
				indent(out) << "}" << endl;
		#if 0
		indent_down();
		indent(out) << "}" << endl;
		#endif
	
		indent(out) << "return -1;" << endl;
		indent_down();
	indent(out) << "}" 	<< endl	<< endl;
}

void	Edf_GenerC::gen_method_simple(ofstream& out, Edf_Type * tt, Edf_Field * tf, bool unk)
{
	// Add the Comment 
	string sComment = "Methods of Integer/Float: ";
	sComment += tf->get_name();
	gen_comment(out, sComment);

	gen_exist_simple(out, tt, tf, unk);
	gen_clear_simple(out, tt, tf, unk);
	
	gen_get_simple(out, tt, tf, unk);
	gen_set_simple(out, tt, tf, unk);

	gen_size_simple(out, tt, tf, unk);
	gen_enc_simple(out, tt, tf, unk);
	gen_dec_simple(out, tt, tf, unk);
}

/*------------------------------------------------------------------------------------------------------------
** Generate the Method Simple HTON
** Integer / 16, 32, 64
*/
void	Edf_GenerC::gen_hton(ofstream& out, Edf_Type * tt)
{
	switch(get_type_id(get_true_type(tt))){
		case EDF_T_S16:
		case EDF_T_U16:
			indent(out) << "#ifdef __USE_EDF_ENDIAN" << endl;
			indent_up();
			indent(out) << "_EDF_HTONS(_xVal);" << endl;
			indent_down();
			indent(out) << "#endif" << endl;
			break;
		
		case EDF_T_S32:
		case EDF_T_U32:
			indent(out) << "#ifdef __USE_EDF_ENDIAN" << endl;
			indent_up();
			indent(out) << "_EDF_HTONL(_xVal);" << endl;
			indent_down();
			indent(out) << "#endif" << endl;
			break;

		case EDF_T_S64:
		case EDF_T_U64:
			indent(out) << "#ifdef __USE_EDF_ENDIAN" << endl;
			indent_up();
			indent(out) << "_EDF_HTONLL(_xVal);" << endl;
			indent_down();
			indent(out) << "#endif" << endl;
			break;

		default:
			break;
	}

	return;
}

void	Edf_GenerC::gen_ntoh(ofstream& out, Edf_Type * tt)
{
	switch(get_type_id(get_true_type(tt))){
		case EDF_T_S16:
		case EDF_T_U16:
			indent(out) << "#ifdef __USE_EDF_ENDIAN" << endl;
			indent_up();
			indent(out) << "_EDF_NTOHS(_xVal);" << endl;
			indent_down();
			indent(out) << "#endif" << endl;
			break;
		
		case EDF_T_S32:
		case EDF_T_U32:
			indent(out) << "#ifdef __USE_EDF_ENDIAN" << endl;
			indent_up();
			indent(out) << "_EDF_NTOHL(_xVal);" << endl;
			indent_down();
			indent(out) << "#endif" << endl;
			break;

		case EDF_T_S64:
		case EDF_T_U64:
			indent(out) << "#ifdef __USE_EDF_ENDIAN" << endl;
			indent_up();
			indent(out) << "_EDF_NTOHLL(_xVal);" << endl;
			indent_down();
			indent(out) << "#endif" << endl;
			break;

		default:
			break;
	}

	return;
}

/*------------------------------------------------------------------------------------------------------------
** Generate the Method Simple AT
** Integer / Double
*/
void	Edf_GenerC::gen_setAt_simple(ofstream& out, Edf_Type * tt, Edf_Field * tf, bool unk)
{
	ostringstream nss;
	nss		<<  tf->get_fid()/EDF_FID_FACTOR;
	string nFid	= nss.str();	
	string tn, fn, tfn;	
	Edf_Type * ttf;
	
	fn 		= tf->get_name();	
	ttf 	= get_true_type(tf->get_type());
	tfn 	= get_typename(ttf);
	if(unk == false){
		ostringstream ass;
		string vFid, pFid, aFid;
		tn   = tt->get_name();
		vFid = "_xPtr->" + fn + "[_idx]";
		pFid = "_xPtr->_presence._fid[" + nFid  +  "]";
		ass	 << "_xPtr->_presence." << fn << "[_idx/" << EDF_FID_FACTOR << "]";
		aFid =  ass.str();

		out << "static _sInt32 _xSetAt_"  << tn 	<< "_"	<< fn 	<< "(" << tn << " * _xPtr, _uInt32 _idx, " << tfn << " _xVal){"	<<	endl;
		indent_up();
		indent(out) << "if(_idx < " << tf->get_repeat() << "){"	<< endl;
		indent_up();
		#if 1
		if(get_type_id(get_true_type(tt)) == EDF_T_UNION){
			indent(out) << "_xClean_" << tn << "(_xPtr);" 		<< endl;
		}
		#endif
		indent(out) << vFid << " = _xVal;"						<< endl;
		indent(out) << pFid << " |= (0X01 << " 	<< tf->get_fid()%EDF_FID_FACTOR << ");" 		<< endl;
		indent(out) << aFid << " |= (0X01 << "	<< "(_idx%" << EDF_FID_FACTOR << "));"			<< endl;
	}
	else{
		CLUE_PREFIX();
		tn 	 = "void ";
		out << "static _sInt32 _xSetAt_"  << prefix << "_"	<< fn	<< "(" << tn << " * _xPtr, _uInt32 _idx, " << tfn << " _xVal){"	<<	endl;
		indent_up();
		CLUE_OFF_IDX_P();		CLUE_OFF_IDX_V();		CLUE_OFF_IDX_A();
		indent(out) << "if(_idx < " << tf->get_repeat() << "){"	<< endl;
		indent_up();
		#if 1
		if(get_type_id(get_true_type(tt)) == EDF_T_UNION){
			indent(out) << "_xClean_" << prefix << "(_xPtr);" << endl;
		}
		#endif
		indent(out) << ELEM_V() << " = _xVal"<< ";"	<< endl;
		indent(out) << ELEM_P() << " |= (0X01 << " 	<< tf->get_fid()%EDF_FID_FACTOR << ");" 	<< endl;
		indent(out) << ELEM_A() << " |= (0X01 << "	<< "(_idx%" << EDF_FID_FACTOR << "));"		<< endl;
	}
	indent(out) << "return (0);" 	<< endl;
	indent_down();
	indent(out) << "}"				<< endl;
	indent(out) << "return (-1);" 	<< endl;
	indent_down();
	out <<	"}"  <<	endl 			<<	endl;
}

void	Edf_GenerC::gen_existAt_simple(ofstream& out, Edf_Type * tt, Edf_Field * tf, bool unk)
{
	ostringstream nss;
	nss 	<<	tf->get_fid()/EDF_FID_FACTOR;
	string nFid = nss.str();	
	string tn, fn, tfn; 
	fn		= tf->get_name();	
	tfn 	= get_typename(get_true_type(tf->get_type()));

	if(unk == false){
		ostringstream ass;
		string vFid, pFid, aFid;
		tn	 = tt->get_name();
		vFid = "_xPtr->" + fn + "[_idx]";
		pFid = "_xPtr->_presence._fid[" + nFid	+  "]";
		ass  << "_xPtr->_presence." << fn << "[_idx/" << EDF_FID_FACTOR << "]";
		aFid =	ass.str();

		out << "static _sInt32 _xExistAt_"  << tn	<< "_"	<< fn	<< "(" << tn << " * _xPtr, _uInt32 _idx){"	<<	endl;
		indent_up();
		indent(out) << "if((_idx < " << tf->get_repeat() << ") && "	<< endl;
		indent(out) << "  (" << pFid << " & (0X01 << "	<< tf->get_fid()%EDF_FID_FACTOR << "))){" 		<< endl;
		indent_up();
			indent(out) << "if(" << aFid << " & (0X01 << "	<< "(_idx%" << EDF_FID_FACTOR << "))){"		<< endl;
	}
	else{
		CLUE_PREFIX();
		tn	 = "void ";
		out << "static _sInt32 _xExistAt_"  << prefix<< "_"	<< fn	<< "(" << tn << " * _xPtr, _uInt32 _idx){"	<<	endl;
		indent_up();
		CLUE_OFF_IDX_P();		CLUE_OFF_IDX_A();
		indent(out) << "if((_idx < " << tf->get_repeat() << ") && "	<< endl;
		indent(out) << "  (" << ELEM_P() << " & (0X01 << "	<< tf->get_fid()%EDF_FID_FACTOR << "))){" 	<< endl;
		indent_up();
			indent(out) << "if(" << ELEM_A() << " & (0X01 << "	<< "(_idx%" << EDF_FID_FACTOR << "))){"	<< endl;
	}
			indent_up();
			indent(out) << "return (1);"<< endl;
			indent_down();
			indent(out) << "}"			<< endl;
			
		indent_down();
		indent(out) << "}" 				<< endl;
		indent(out) << "return (0);" 	<< endl;
	indent_down();
	out <<	"}" <<	endl 			<<	endl;
}

void	Edf_GenerC::gen_clearAt_simple(ofstream& out, Edf_Type * tt, Edf_Field * tf, bool unk)
{
	ostringstream nss;
	nss 	<<	tf->get_fid()/EDF_FID_FACTOR;
	string nFid = nss.str();	
	string tn, fn, tfn; 
	fn		= tf->get_name();	
	tfn 	= get_typename(get_true_type(tf->get_type()));

	if(unk == false){
		ostringstream ass;
		string vFid, pFid, aFid;
		tn	 = tt->get_name();
		vFid = "_xPtr->" + fn + "[_idx]";
		pFid = "_xPtr->_presence._fid[" + nFid	+  "]";
		ass  << "_xPtr->_presence." << fn << "[_idx/" << EDF_FID_FACTOR << "]";
		aFid =	ass.str();

		out << "static void _xClearAt_"  << tn	<< "_"	<< fn	<< "(" << tn << " * _xPtr, _uInt32 _idx){"	<<	endl;
		indent_up();
		indent(out) << "if((_idx < " << tf->get_repeat() << ") && "	<< endl;
		indent(out) << "  (" << pFid << " & (0X01 << "	<< tf->get_fid()%EDF_FID_FACTOR << "))){" 		<< endl;
		indent_up();
			indent(out) <<  aFid << " &= ~(0X01 << "	<< "(_idx%" << EDF_FID_FACTOR << "));"		<< endl;
	}
	else{
		CLUE_PREFIX();
		tn	 = "void ";
		out << "static void _xClearAt_"  << prefix<< "_"	<< fn	<< "(" << tn << " * _xPtr, _uInt32 _idx){"	<<	endl;
		indent_up();
		CLUE_OFF_IDX_P();		CLUE_OFF_IDX_A();
		indent(out) << "if((_idx < " << tf->get_repeat() << ") && "	<< endl;
		indent(out) << "  (" << ELEM_P() << " & (0X01 << "	<< tf->get_fid()%EDF_FID_FACTOR << "))){" 	<< endl;
		indent_up();
			indent(out) <<  ELEM_A() << " &= ~(0X01 << "	<< "(_idx%" << EDF_FID_FACTOR << "));"	<< endl;
	}
	indent_down();
	indent(out) << "}" 				<< endl;
	indent(out) << "return;" 	<< endl;
	indent_down();
	out <<	"}" <<	endl 			<<	endl;
}


void	Edf_GenerC::gen_getAt_simple(ofstream& out, Edf_Type * tt, Edf_Field * tf, bool unk)
{
	ostringstream nss;
	nss 	<<	tf->get_fid()/EDF_FID_FACTOR;
	string nFid = nss.str();	
	string tn, fn, tfn; 
	Edf_Type * ttf;

	fn		= tf->get_name();	
	ttf 	= get_true_type(tf->get_type());
	tfn 	= get_typename(ttf);
	if(unk == false){\
		string vFid, pFid, aFid;
		ostringstream ass;
		tn	 = tt->get_name();
		vFid = "_xPtr->" + fn + "[_idx]";
		pFid = "_xPtr->_presence._fid[" + nFid	+  "]";
		ass  << "_xPtr->_presence." << fn << "[_idx/" << EDF_FID_FACTOR << "]";
		aFid =	ass.str();

		out << "static " << tfn << " _xGetAt_"  << tn	<< "_"	<< fn	<< "(" 	<< tn << " * _xPtr,  _uInt32 _idx){"	<<	endl;
		indent_up();
		indent(out) << tfn << " _xVal;" << endl;
		indent(out) << "if(_idx < " << tf->get_repeat() << "){"	<< endl;
		indent_up();
		indent(out) << "_xVal = (" << tfn <<")(" << vFid << ");"	<< endl;
		indent(out) << "return _xVal;"	<< endl;
	}
	else{
		CLUE_PREFIX();
		tn	 = "void ";
		out << "static " << tfn << " _xGetAt_"  << prefix<< "_"	<< fn	<< "(" 	<< tn << " * _xPtr,  _uInt32 _idx){"	<<	endl;
		indent_up();
		CLUE_OFF_IDX_V();
		indent(out) << "if(_idx < " << tf->get_repeat() << "){"	<< endl;
		indent_up();
		indent(out) << "_xVal = "<< ELEM_V() << ";"				<< endl;
		indent(out) << "return _xVal;"	<< endl;
	}
	indent_down();
	indent(out) << "}" 							<< endl;
	indent(out) << "return (" << tfn <<")(0);" 	<< endl;
	indent_down();
	out <<	"}" <<	endl 	<<	endl;
}

void	Edf_GenerC::gen_decAt_simple(ofstream& out, Edf_Type * tt, Edf_Field * tf, bool unk)
{
	string sComment;
	ostringstream nss;	
	string tn, fn, tfn, pFid, aFid; 
	int 		tid;
	Edf_Type *	ttf;

	nss 	<<	tf->get_fid()/EDF_FID_FACTOR;
	string 	nFid = nss.str();	

	ttf 	= get_true_type(tf->get_type());
	tid 	= get_type_id(ttf);
	fn		= tf->get_name();	
	tfn 	= get_typename(get_true_type(tf->get_type()));
	
	if(unk == false){
		ostringstream ass; 
		string vFid;
		tn	 = tt->get_name();
		ass  << "_xPtr->_presence." << fn;
		vFid = "_xPtr->" + fn;
		aFid =	ass.str();
		pFid = "_xPtr->_presence._fid[" + nFid	+  "]";
				
		out << "static _sInt32 _xDecAt_"<< tn	<< "_"	<< fn	<< "(" << tn	<< " * _xPtr, _uInt8 * _oPtr, _sInt32 _oLen, _sInt32 * _oStatus){" <<	endl;
		indent_up();
			/* Add the Val Define */
			indent(out) << tfn << " _xVal;" << endl;
			indent(out) << "_uInt32 _jdx, _idx, _iCnt, _mCnt, _mask, _sum;" 	<< endl;
			indent(out) << tfn << "*_iPtr, *_sPtr, *_vPtr;" 					<< endl;
			indent(out) << "_uInt32 *_aPtr, *_hPtr;"					<< endl;
			indent(out) << "_sPtr = ("	<< tfn 	<< "*)(" << vFid <<");"	<< endl; 
	}
	else{
		CLUE_PREFIX();
		tn	 = "void ";
		out << "static _sInt32 _xDecAt_"<< prefix<< "_"	<< fn	<< "(" << tn	<< " * _xPtr, _uInt8 * _oPtr, _sInt32 _oLen, _sInt32 * _oStatus){"	<<	endl;
		indent_up();
			/* Add the Val Define */
			indent(out) << tfn << " _xVal;" << endl;
			indent(out) << "_uInt32 _jdx, _idx, _iCnt, _mCnt, _mask, _sum;" 	<< endl;
			indent(out) << tfn << "*_iPtr, *_sPtr, *_vPtr;" 					<< endl;
			indent(out) << "_uInt32 *_aPtr, *_hPtr;"			<< endl;
			CLUE_OFF_IDX_P();			CLUE_OFF_IDX_VBEG();	 			CLUE_OFF_IDX_ABEG();
			pFid = ELEM_P();	aFid = ELEM_APTR();
				
			indent(out) << "_sPtr = "	<<	ELEM_VPTR() <<";"	<< endl;
	}
			// Add the Comment 
			sComment = "Check Type : ";
			sComment += tf->get_name();
			gen_comment(out, sComment);
			
			indent(out) << "_mask = *(_oPtr + 1);"	<< endl;
			indent(out) << "if((_mask >> 4) == "	<< tid << "){"	<< endl;
			indent_up();
				_ENDVAL_NTOHL("*((_uInt32 *)(_oPtr + 2))");
				_ENDVAL_NTOHL("*((_uInt32 *)(_oPtr + 6))");
				
				indent(out) << "_iCnt = *((_uInt32*)(_oPtr + 2));" 			<< endl;
				indent(out) << "_mCnt = *((_uInt32*)(_oPtr + 6));" 			<< endl;
				indent(out) << "_iPtr = ("	<< tfn	<< "*)(_oPtr + 10);"	<< endl;
				indent(out) << "_sum  = 10 + _iCnt * (" << get_simple_len(ttf) << ");" << endl; 
				indent(out) << "_hPtr = (_uInt32 *)(_oPtr + _sum);" 		<< endl;

				indent(out) << "if((_iCnt > 0) && (_iCnt <= " << tf->get_repeat() << ")){" << endl;
				indent_up();

					indent(out) << "_sum  += (_mCnt * 8);" << endl; 

					// Add the Comment 
					sComment = "Presence Of A: ";
					sComment += tf->get_name();
					gen_comment(out, sComment);
					indent(out) << "_aPtr = "	<< aFid <<";"		  << endl; 

					indent(out) << "for(_jdx = 0; _jdx < _mCnt; _jdx++, _hPtr+=2){" << endl;
					indent_up();
						_ENDVAL_NTOHL("*(_hPtr)");
						_ENDVAL_NTOHL("*(_hPtr + 1)");
						indent(out) << "_mask = *(_hPtr + 1);  _aPtr[*_hPtr] = _mask; _vPtr = _sPtr + (*_hPtr) * 32;" << endl;
						indent(out) << "for(_idx = 0; _idx < 32; _idx++, _mask >>= 1){"	<< endl;
						indent_up();
							indent(out) << "if(_mask == 0){" << endl;
							indent_up();
								indent(out) << "break;" << endl;
							indent_down();
							indent(out) << "}" << endl;
							indent(out) << "if(_mask & 0X01){"	<< endl; 
							indent_up();
								indent(out) << "_xVal = *(_iPtr);" << endl;
								/* Add Endian Support */
								gen_ntoh(out, ttf);
								indent(out) << "*(_vPtr + _idx) = _xVal; _iPtr += 1; _iCnt -= 1;"	<< endl;
							indent_down();
							indent(out) << "}"			<< endl;
						indent_down();
						indent(out) << "}"			<< endl;
					indent_down();
					indent(out) << "}"			<< endl;
					
					indent(out) << pFid << " |= (0X01 << " << tf->get_fid()%EDF_FID_FACTOR << ");" 	<< endl;
					
					indent(out) << "return (_sum);" 	<< endl;

					indent_down();
					indent(out) << "} else {" << endl;
					indent_up();
						indent(out) << "if(_oStatus)  (*_oStatus) |= (_EDF_E_CRT | _EDF_E_OOR); " << endl;
						indent(out) << "return -1;" << endl;
					indent_down();
					indent(out) << "}" << endl;
				
				indent_down();
				indent(out) << "} else {" 			<< endl;
				indent_up();
					indent(out) << "if(_oStatus)  (*_oStatus) |= _EDF_E_UNK; " << endl;
					indent(out) << "return " << _dec_unknown <<"(_oPtr, _oLen);" << endl;
				indent_down();
				indent(out) << "}" 			<< endl;
				
				indent(out) << "return -1;" << endl;
			indent_down();
			indent(out) << "}"	<< endl 
								<< endl;
}

void	Edf_GenerC::gen_encAt_simple(ofstream& out, Edf_Type * tt, Edf_Field * tf, bool unk)
{
	string sComment;
	ostringstream nss;	
	string tn, fn, tfn; 
	Edf_Type *	ttf;
	ttf 	= get_true_type(tf->get_type());
	fn		= tf->get_name();	
	tfn 	= get_typename(get_true_type(tf->get_type()));
	
	int  repeat = (tf->get_repeat() + EDF_FID_FACTOR - 1)/EDF_FID_FACTOR;
	
	if(unk == false){
		ostringstream ass; 
		string vFid, aFid;
		tn	 = tt->get_name();
		ass  << "_xPtr->_presence." << fn;
		vFid = "_xPtr->" + fn;
		aFid =	ass.str();
		
		out << "static _sInt32 _xEncAt_"<< tn		<< "_"	<< fn	<< "(" << tn	<< " * _xPtr, _uInt8 * _oPtr, _sInt32 _oLen, _sInt32 * _oStatus){" <<	endl;
		indent_up();
			/* Add the Val Define */
			indent(out) << tfn << " _xVal;" << endl;
			indent(out) << "_uInt32  _idx, _jdx, _mCnt, _iCnt, _mask;" 		<< endl;
			indent(out) << tfn << "*_iPtr, *_vPtr;" 		<< endl;
			indent(out) << "_uInt32 *_aPtr, *_hPtr;"		<< endl;
			indent(out) << 	"_uInt32 _aTmp[" 	<<  repeat*2	<< "];" 	<< endl;

			indent(out) << "_vPtr = ("	<< tfn	<< "*)(" << vFid <<");" 			<< endl; 
			indent(out) << "_aPtr = "	<< aFid <<";"								<< endl; 
	}
	else{
		CLUE_PREFIX();
		tn	 = "void ";
		out << "static _sInt32 _xEncAt_"<< prefix	<< "_"	<< fn	<< "(" << tn	<< " * _xPtr, _uInt8 * _oPtr, _sInt32 _oLen, _sInt32 * _oStatus){"	<<	endl;
		indent_up();
			/* Add the Val Define */
			indent(out) << tfn << " _xVal;" << endl;
			indent(out) << "_uInt32  _idx, _jdx, _mCnt, _iCnt, _mask;"		<< endl;
			indent(out) << tfn << "*_iPtr, *_vPtr;" 		<< endl;
			indent(out) << "_uInt32 *_aPtr, *_hPtr;"		<< endl;
			indent(out) << 	"_uInt32 _aTmp[" 	<<  repeat*2 	<< "];" 	<< endl;

			CLUE_OFF_IDX_VBEG();	 		CLUE_OFF_IDX_ABEG();
			indent(out) << "_vPtr = "	<<	ELEM_VPTR() <<";"							<< endl;
			indent(out) << "_aPtr = "	<<	ELEM_APTR() <<";"							<< endl;
	}
			indent(out) << "if( EDF_PACK_MIN <= _oLen){"	<< endl;
			indent_up();
				indent(out) << "*_oPtr 		 = "	<< (tf->get_fid()<< 0X01)	<< " | (0X01);" 	<< endl; 
				indent(out) << "*(_oPtr + 1) = "	<< (get_type_id(ttf)<< 0X04)<< ";"	<< endl; 
				indent(out) << "_hPtr = _aTmp;"		<< endl; 
				indent(out) << "_iPtr = (" << tfn << "*) (_oPtr + 10);" 	<< endl;
				indent(out) << "_oLen-= 10;"			<< endl;
				
				indent(out) << "for(_jdx = 0, _iCnt = 0, _mCnt = 0; _jdx < " << repeat << "; _jdx++, _aPtr++, _vPtr+=32){" << endl;
				indent_up();
					indent(out) << "if((*_aPtr) != 0){" << endl;
					indent_up();
						indent(out) << "*_hPtr = _jdx; *(_hPtr + 1) = *_aPtr;  " << endl; 
						_ENDVAL_HTONL("*_hPtr");
						_ENDVAL_HTONL("*(_hPtr + 1)");
						indent(out) << "_hPtr += 2; _mCnt += 1; _oLen -= 8;" 	<< endl;
						
						indent(out) << "for(_idx = 0, _mask =(*_aPtr); _idx < 32; _idx++, _mask >>= 1){" 	<< endl;
						indent_up();
							indent(out) << "if(_mask == 0){" << endl;
							indent_up();
								indent(out) << "break;" << endl;
							indent_down();
							indent(out) << "}" << endl;
							indent(out) << "if(_mask & 0X01){"	<< endl; 
							indent_up();
								indent(out) << "_oLen -= (" << get_simple_len(ttf) 	<< ");" 	<< endl; 
								indent(out)<< "if(_oLen > 0){"	<< endl; 
								indent_up();
									indent(out) << "_xVal = *(_vPtr + _idx);" << endl;
									/* Add Endian Support */
									gen_hton(out, ttf);
									indent(out) << "*(_iPtr) = _xVal;" << endl; 

									indent(out) << " _iPtr += 1; _iCnt += 1;" 	<< endl;
								indent_down();
								indent(out) << "} else {"		<< endl;
								indent_up();
								
									// Add the Comment 
									sComment = "Check OOM : ";
									sComment += tf->get_name();
									gen_comment(out, sComment);
									
									indent(out) << "if(_oStatus)  (*_oStatus) |= _EDF_E_OOM; " << endl;
									indent(out) << "return 0;" << endl;
								indent_down();
								indent(out) << "}"			<< endl;
							indent_down();
							indent(out) << "}"			<< endl;
						indent_down();
						indent(out) << "}"			<< endl;
					indent_down();
					indent(out) << "}"			<< endl;
				indent_down();
				indent(out) << "}"	<< endl;

				indent(out) << "if( _iCnt > 0){" << endl;
				indent_up();
					indent(out) << "_hPtr = (_uInt32 *)_iPtr;"	<< endl; 
					indent(out) << "_xEdfCopy(_hPtr, _aTmp, _mCnt * 8);"<< endl;
					indent(out) << "*((_uInt32 *)(_oPtr + 2)) = _iCnt;" << endl;
					indent(out) << "*((_uInt32 *)(_oPtr + 6)) = _mCnt;" << endl;
					_ENDVAL_HTONL("*((_uInt32 *)(_oPtr + 2))");
					_ENDVAL_HTONL("*((_uInt32 *)(_oPtr + 6))");
					
					indent(out) << "return ((_uInt8*)_iPtr - (_uInt8*)_oPtr + (_mCnt * 8));" << endl;
				indent_down();
				indent(out) << "}"		<< endl;

			indent_down();
			indent(out) << "}"			<< endl;
	indent(out) << "return (0);"		<< endl;
	indent_down();	
	out <<	"}"			<<	endl		<< endl;	
}


void	Edf_GenerC::gen_sizeAt_simple(ofstream& out, Edf_Type * tt, Edf_Field * tf, bool unk)
{
	ostringstream nss;	
	string tn, fn, tfn; 
	Edf_Type *	ttf;
	ttf 	= get_true_type(tf->get_type());
	fn		= tf->get_name();	
	tfn 	= get_typename(get_true_type(tf->get_type()));
	if(unk == false){
		ostringstream ass; 
		string vFid, aFid;
		tn	 = tt->get_name();
		ass  << "_xPtr->_presence." << fn;
		vFid = "_xPtr->" + fn;
		aFid =	ass.str();
		
		out << "static _sInt32 _xSizeAt_"<< tn		<< "_"	
			<< fn	<< "(" << tn	<< " * _xPtr){" <<	endl;
		indent_up();
			indent(out) << "_uInt32  _idx, _jdx, _iCnt, _mCnt, _mask;"		<< endl;
			indent(out) << "_uInt32 *_aPtr;"		<< endl;
			indent(out) << "_aPtr = "	<< aFid <<";"								<< endl; 
	}
	else{
		CLUE_PREFIX();
		tn	 = "void ";
		out << "static _sInt32 _xSizeAt_"<< prefix	<< "_"	
			<< fn	<< "(" << tn	<< " * _xPtr){"	<<	endl;
		indent_up();
			indent(out) << "_uInt32  _idx, _jdx, _iCnt, _mCnt, _mask;"		<< endl;
			CLUE_OFF_IDX_ABEG();	
			indent(out) << "_uInt32 *_aPtr = "	<<  ELEM_APTR() <<";"					<< endl;
	}
				int  repeat = (tf->get_repeat() + EDF_FID_FACTOR - 1)/EDF_FID_FACTOR;
				indent(out) << "for(_jdx = 0, _iCnt = 0, _mCnt = 0; _jdx < " << repeat << "; _jdx++, _aPtr++){" << endl;
				indent_up();
					indent(out) << "if((*_aPtr) != 0){" << endl;
					indent_up();
						indent(out) << "_mCnt += 1;" 	<< endl;
						indent(out) << "for(_idx = 0, _mask =(*_aPtr); _idx < 32; _idx++, _mask >>= 1){" << endl;
						indent_up();
							indent(out) << "if(_mask == 0){" << endl;
							indent_up();
								indent(out) << "break;" << endl;
							indent_down();
							indent(out) << "}" << endl;
							indent(out) << "if(_mask & 0X01){"	<< endl; 
							indent_up();
								indent(out) << "_iCnt  += 1;"								<< endl; 
							indent_down();
							indent(out) << "}"			<< endl;
						indent_down();
						indent(out) << "}"			<< endl;
					indent_down();
					indent(out) << "}"			<< endl;
				indent_down();
				indent(out) << "}"	<< endl;

				indent(out) << "if( _iCnt > 0){" << endl;
				indent_up();
					indent(out) << "return (_iCnt * (" << get_simple_len(ttf) << ") + _mCnt * 8 + 10);" 	<< endl; 
				indent_down();
				indent(out) << "}"		<< endl;

	indent(out) << "return (0);"		<< endl;
	indent_down();	
	out <<	"}"			
		<<	endl		
		<< endl;	
}

void	Edf_GenerC::gen_count_simple(ofstream& out, Edf_Type * tt, Edf_Field * tf, bool unk)
{
	ostringstream nss;	
	string tn, fn, tfn; 
	Edf_Type *	ttf;
	ttf 	= get_true_type(tf->get_type());
	fn		= tf->get_name();	
	tfn 	= get_typename(get_true_type(tf->get_type()));
	if(unk == false){
		ostringstream ass; 
		string vFid, aFid;
		tn	 = tt->get_name();
		ass  << "_xPtr->_presence." << fn;
		vFid = "_xPtr->" + fn;
		aFid =	ass.str();
		
		out << "static _sInt32 _xCount_"<< tn		<< "_"	
			<< fn	<< "(" << tn	<< " * _xPtr){" <<	endl;
		indent_up();
			indent(out) << "_sInt32 _idx, _jdx;"	<< endl;
			indent(out) << "_uInt32 _cnt, _mask;" 		<< endl;
			indent(out) << "_uInt32 *_aPtr;"			<< endl;
			indent(out) << "_aPtr = "	<< aFid <<";"	<< endl; 
	}
	else{
		CLUE_PREFIX();
		tn	 = "void ";
		out << "static _sInt32 _xCount_"<< prefix	<< "_"	
			<< fn	<< "(" << tn	<< " * _xPtr){"	<<	endl;
		indent_up();
			indent(out) << "_sInt32 _idx, _jdx;" 	<< endl;
			indent(out) << "_uInt32 _cnt, _mask;" 		<< endl;

			CLUE_OFF_IDX_ABEG();	
			indent(out) << "_uInt32 *_aPtr = "	<<  ELEM_APTR() <<";"					<< endl;
	}
			int  repeat = (tf->get_repeat() + EDF_FID_FACTOR - 1)/EDF_FID_FACTOR;
			indent(out) << "for(_jdx = 0, _cnt = 0; _jdx < " << repeat << "; _jdx++, _aPtr++){" << endl;
			indent_up();
				indent(out) << "if((*_aPtr) != 0){" << endl;
				indent_up();
					indent(out) << "for(_idx = 0, _mask =(*_aPtr); _idx < 32; _idx++, _mask >>= 1){" << endl;
					indent_up();
						indent(out) << "if(_mask == 0){" << endl;
						indent_up();
							indent(out) << "break;" << endl;
						indent_down();
						indent(out) << "}" << endl;
						indent(out) << "if(_mask & 0X01){"	<< endl; 
						indent_up();
							indent(out) << "_cnt  += 1;"								<< endl; 
						indent_down();
						indent(out) << "}"			<< endl;
					indent_down();
					indent(out) << "}"			<< endl;
				indent_down();
				indent(out) << "}"			<< endl;
			indent_down();
			indent(out) << "}"	<< endl;

		indent(out) << "return (_cnt);"		<< endl;
	indent_down();	
	out <<	"}"			
		<<	endl		
		<< endl;	
}


void	Edf_GenerC::gen_methodAt_simple(ofstream& out, Edf_Type * tt, Edf_Field * tf, bool unk)
{
	// Add the Comment 
	string sComment = "Methods of Integer/Float[]: ";
	sComment += tf->get_name();
	gen_comment(out, sComment);

	gen_existAt_simple(out, tt, tf, unk);
	gen_clearAt_simple(out, tt, tf, unk);
	
	gen_getAt_simple(out, tt, tf, unk);
	gen_setAt_simple(out, tt, tf, unk);

	gen_sizeAt_simple(out, tt, tf, unk);
	gen_encAt_simple(out, tt, tf, unk);
	gen_decAt_simple(out, tt, tf, unk);

	gen_count_simple(out, tt, tf, unk);
}

/*------------------------------------------------------------------------------------------------------------
** Generate the Method Sequence
*/
void	Edf_GenerC::gen_set_seq(ofstream& out, Edf_Type * tt, Edf_Field * tf, bool unk)
{
	ostringstream nss;
	nss 	<<	tf->get_fid()/EDF_FID_FACTOR;
	string nFid = nss.str();	
	string tn, fn, tfn; 
	fn		= tf->get_name();	
	tfn 	= get_typename(get_true_type(tf->get_type()));
	if(unk == false){
		string vFid, pFid;
		tn	 = tt->get_name();
		vFid = "_xPtr->" + fn;
		pFid = "_xPtr->_presence._fid[" + nFid	+  "]";
		out << "static _sInt32 _xSet_" 	<< tn		<< "_"		<< fn	<< "(" << tn	<< " * _xPtr){"		<<	endl;
		indent_up();
		#if 1
		if(get_type_id(get_true_type(tt)) == EDF_T_UNION){
			indent(out) << "_xClean_" << tn << "(_xPtr);" << endl;
		}
		#endif
		indent(out) << pFid <<	" |= (0X01 << " << tf->get_fid()%EDF_FID_FACTOR << ");" 					<< endl;
	}
	else{
		CLUE_PREFIX();
		tn	 = "void ";
		out << "static _sInt32 _xSet_" 	<< prefix	<< "_"		<< fn	<< "(" << tn	<< " * _xPtr){"		<<	endl;
		indent_up();
		
		CLUE_OFF_P();
		#if 1
		if(get_type_id(get_true_type(tt)) == EDF_T_UNION){
			indent(out) << "_xClean_" << prefix << "(_xPtr);" << endl;
		}
		#endif
		indent(out) << ELEM_P() 	<<	" |= (0X01 << " << tf->get_fid()%EDF_FID_FACTOR << ");" 				<< endl;
	}
	indent(out) << "return (0);" << endl;
	indent_down();
	out <<	"}"  
		<<	endl 
		<<	endl;
}

void	Edf_GenerC::gen_exist_seq(ofstream& out, Edf_Type * tt, Edf_Field * tf, bool unk)
{	
	ostringstream nss;
	nss 	<<	tf->get_fid()/EDF_FID_FACTOR;
	string nFid = nss.str();	
	string tn, fn, tfn; 
	fn		= tf->get_name();	
	tfn 	= get_typename(get_true_type(tf->get_type()));\
	if(unk == false){
		string vFid, pFid;
		tn	 = tt->get_name();
		vFid = "_xPtr->" + fn;
		pFid = "_xPtr->_presence._fid[" + nFid	+  "]";
		out << "static _sInt32 _xExist_" 	<< tn		<< "_"		<< fn	<< "(" << tn	<< " * _xPtr){"	<<	endl;
		indent_up();
		indent(out) << "if(" << pFid << " & (0X01 << "	<< tf->get_fid()%EDF_FID_FACTOR << ")){" 			<< endl;
	}
	else{
		CLUE_PREFIX();
		tn	 = "void ";
		out << "static _sInt32 _xExist_" 	<< prefix	<< "_"		<< fn	<< "(" << tn	<< " * _xPtr){"	<<	endl;
		indent_up();
		CLUE_OFF_P();
		indent(out) << "if(" << ELEM_P() << " & (0X01 << "	<< tf->get_fid()%EDF_FID_FACTOR << ")){"		<< endl;
	}
		indent_up();
		indent(out) << "return (1);"	<< endl;
		indent_down();
		indent(out) << "}" 				<< endl;
	indent(out) << "return (0);" 	<< endl;
	indent_down();	
	out <<	"}" 	
		<<	endl 	
		<<	endl;	
}

void	Edf_GenerC::gen_clear_seq(ofstream& out, Edf_Type * tt, Edf_Field * tf, bool unk)
{	
	ostringstream nss;
	nss 	<<	tf->get_fid()/EDF_FID_FACTOR;
	string nFid = nss.str();	
	string tn, fn, tfn; 
	fn		= tf->get_name();	
	tfn 	= get_typename(get_true_type(tf->get_type()));\
	if(unk == false){
		string vFid, pFid;
		tn	 = tt->get_name();
		vFid = "_xPtr->" + fn;
		pFid = "_xPtr->_presence._fid[" + nFid	+  "]";
		out << "static void _xClear_" 	<< tn		<< "_"		<< fn	<< "(" << tn	<< " * _xPtr){"	<<	endl;
		indent_up();
		indent(out) << pFid << " &= ~(0X01 << "	<< tf->get_fid()%EDF_FID_FACTOR << ");" 			<< endl;
	}
	else{
		CLUE_PREFIX();
		tn	 = "void ";
		out << "static void _xClear_" 	<< prefix	<< "_"		<< fn	<< "(" << tn	<< " * _xPtr){"	<<	endl;
		indent_up();
		CLUE_OFF_P();
		indent(out) << ELEM_P() << " &= ~(0X01 << "	<< tf->get_fid()%EDF_FID_FACTOR << ");"		<< endl;
	}
	indent(out) << "return;" 	<< endl;
	indent_down();	
	out <<	"}" 	
		<<	endl 	
		<<	endl;	
}


void	Edf_GenerC::gen_get_seq(ofstream& out, Edf_Type * tt, Edf_Field * tf, bool unk)
{
	ostringstream nss;
	nss 	<<	tf->get_fid()/EDF_FID_FACTOR;
	string nFid = nss.str();	
	string tn, fn, tfn; 
	fn		= tf->get_name();	
	tfn 	= get_typename(get_true_type(tf->get_type()));
	if(tfn == ""){
		tfn = "void ";
	}
	if(unk == false){
		string vFid, pFid;
		tn	 = tt->get_name();
		vFid = "_xPtr->" + fn;
		pFid = "_xPtr->_presence._fid[" + nFid	+  "]";
		out << "static " 	<< tfn << "*_xGet_"	<< tn		<< "_"			<< fn	<< "(" 	<< tn	<< " * _xPtr){"	<<	endl;
		indent_up();
		indent(out) << "return &(" << vFid << ");" << endl;
	}
	else{
		CLUE_PREFIX();
		tn	 = "void ";
		out << "static " 	<< tfn << "*_xGet_"	<< prefix	 	<< "_"		<< fn	<< "(" 	<< tn	<< " * _xPtr){"	<<	endl;
		indent_up();
		CLUE_OFF_V();
		indent(out) << "return " << ELEM_VPTR() << ";" << endl;
	}
	indent_down();
	out <<	"}"  
		<<	endl 
		<<	endl;
}


void	Edf_GenerC::gen_enc_seq(ofstream& out, Edf_Type * tt, Edf_Field * tf, bool unk)
{
	ostringstream nss;
	nss 	<<	tf->get_fid()/EDF_FID_FACTOR;
	string 	nFid = nss.str();	
	string 	tn, fn, tfn; 	
	Edf_Type * 	ttf;
	
	ttf 	= get_true_type(tf->get_type());
	fn		= tf->get_name();	
	tfn 	= get_typename(get_true_type(tf->get_type()));
	if(unk == false){
		string vFid;
		tn	 = tt->get_name();
		vFid = "_xPtr->" + fn;
		out << "static _sInt32 _xEnc_"	<< tn		<< "_"	<< fn	<< "(" << tn	<< " * _xPtr, _uInt8 * _oPtr, _sInt32 _oLen, _sInt32 * _oStatus){"	<<	endl;
		indent_up();
			indent(out) << "_sInt32 _len;" << endl; 
			indent(out) << tfn << " * _iPtr = (" << tfn <<" *)(&(" << vFid << "));" 	<< endl;
	}
	else{
		CLUE_PREFIX();
		tn	 = "void ";
		out << "static _sInt32 _xEnc_"	<< prefix	 << "_"	<< fn	<< "(" << tn	<< " * _xPtr, _uInt8 * _oPtr, _sInt32 _oLen, _sInt32 * _oStatus){"	<<	endl;
		indent_up();
			indent(out) << "_sInt32 _len;" << endl; 
			CLUE_OFF_V();
			indent(out) << tfn << " * _iPtr = (" << tfn <<" *)(" << ELEM_VPTR() << ");" 	<< endl;
	}
		indent(out) << "if((_len =_iPtr->len) >= 0){" 		<< endl;
		indent_up();
			indent(out) << "if(( 7 + _len ) <= _oLen){" << endl;
			indent_up();
				indent(out) << "*_oPtr = "			<< (tf->get_fid()<< 0X01)	<< ";" 		<< endl;
				indent(out) << "*(_oPtr + 1) = "	<< (get_type_id(ttf)<< 0X04)<< ";" 		<< endl;
				indent(out) << "*((_uInt32 *)(_oPtr + 2)) = _len + 5;" 						<< endl;
				_ENDVAL_HTONL("*((_uInt32 *)(_oPtr + 2))");
				indent(out) << "_oPtr += 6;" 												<< endl;
				indent(out) << "if(_iPtr->val){" << endl;
				indent_up();
					indent(out) << "_xEdfCopy(_oPtr, _iPtr->val, _len);" 						<< endl;
				indent_down();
				indent(out) << "}" << endl;
				indent(out) << "*(_oPtr + _len) = 0;"										<< endl;
				indent(out) << "return (_len + 7);"											<< endl;
			indent_down();
			indent(out) << "} else {" << endl;
			indent_up();
				// Add the Comment 
				string sComment = "Check OOM : ";
				sComment += tf->get_name();
				gen_comment(out, sComment);
				
				indent(out) << "if(_oStatus)  (*_oStatus) |= _EDF_E_OOM; " << endl;
				indent(out) << "return 0;" << endl;
			indent_down();
			indent(out) << "}" 	<< endl;
		indent_down();
		indent(out) << "}" 		<< endl;
		
	indent(out) << "return (0);"<< endl;
	indent_down();
	out <<	"}" 
		<<	endl 
		<<	endl;
}


void	Edf_GenerC::gen_size_seq(ofstream& out, Edf_Type * tt, Edf_Field * tf, bool unk)
{
	ostringstream nss;
	nss 	<<	tf->get_fid()/EDF_FID_FACTOR;
	string 	nFid = nss.str();	
	string 	tn, fn, tfn; 	
	Edf_Type * 	ttf;
	
	ttf 	= get_true_type(tf->get_type());
	fn		= tf->get_name();	
	tfn 	= get_typename(get_true_type(tf->get_type()));
	if(unk == false){
		string vFid;
		tn	 = tt->get_name();
		vFid = "_xPtr->" + fn;
		out << "static _sInt32 _xSize_"	<< tn		<< "_"	
			<< fn	<< "(" << tn	<< " * _xPtr){"	<<	endl;
		indent_up();
			indent(out) << "_sInt32 _len;" << endl; 
			indent(out) << tfn << " * _iPtr = (" << tfn <<" *)(&(" << vFid << "));" 	<< endl;
	}
	else{
		CLUE_PREFIX();
		tn	 = "void ";
		out << "static _sInt32 _xSize_"	<< prefix	 << "_"	
			<< fn	<< "(" << tn	<< " * _xPtr){"	<<	endl;
		indent_up();
			indent(out) << "_sInt32 _len;" << endl; 
			CLUE_OFF_V();
			indent(out) << tfn << " * _iPtr = (" << tfn <<" *)(" << ELEM_VPTR() << ");" 	<< endl;
	}
		indent(out) << "if((_len =_iPtr->len) >= 0){" 		<< endl;
		indent_up();
			indent(out) << "return (_len + 7);"				<< endl;
		indent_down();
		indent(out) << "}" 		<< endl;
		
	indent(out) << "return (0);"<< endl;
	indent_down();
	out <<	"}" 
		<<	endl 
		<<	endl;
}


void	Edf_GenerC::gen_dec_seq(ofstream& out, Edf_Type * tt, Edf_Field * tf, bool unk)
{
	ostringstream nss;
	nss 	<<	tf->get_fid()/EDF_FID_FACTOR;
	string 	nFid = nss.str();	
	string 	tn, fn, tfn,  pFid; 	
	Edf_Type * 	ttf;
	int 		tid;
	
	ttf 	= get_true_type(tf->get_type());
	tid 	= get_type_id(ttf);
	fn		= tf->get_name();	
	tfn 	= get_typename(get_true_type(tf->get_type()));
	if(unk == false){
		string vFid;
		tn	 = tt->get_name();
		vFid = "_xPtr->" + fn;
		pFid = "_xPtr->_presence._fid[" + nFid	+  "]";
		out << "static _sInt32 _xDec_"	<< tn		<< "_"	<< fn	<< "(" << tn	<< " * _xPtr, _uInt8 * _oPtr, _sInt32 _oLen, _sInt32 * _oStatus){"	<<	endl;
		indent_up();
			indent(out) << "_uInt8	_iType;" << endl;
			indent(out) << "_sInt32 _iLen;"  << endl;

			indent(out) << "_iType = *(_oPtr + 1);" << endl;
			indent(out) << "if((_iType >> 4) == "	<< tid << "){" 	<< endl;
			indent_up();
				_ENDVAL_NTOHL("*((_sInt32*)(_oPtr + 2))");
				indent(out) << "_iLen = *((_sInt32*)(_oPtr + 2));" 	<< endl;
				indent(out) << "if(_iLen >= 5){" << endl;
				indent_up();
					indent(out) << vFid << ".len= _iLen - 5;" << endl;
					indent(out) << vFid << ".val= (char * )(_oPtr + 6);" << endl;
	}
	else{
		CLUE_PREFIX();
		tn	 = "void ";
		out << "static _sInt32 _xDec_"	<< prefix	 << "_"	<< fn	<< "(" << tn	<< " * _xPtr, _uInt8 * _oPtr, _sInt32 _oLen, _sInt32 * _oStatus){"	<<	endl;
		indent_up();
			indent(out) << "_uInt8	_iType;" << endl;
			indent(out) << "_sInt32 _iLen;"  << endl;
			CLUE_OFF_V();			CLUE_OFF_P();
			pFid = ELEM_P() ;
			
			indent(out) << "_iType = *(_oPtr + 1);" << endl;
			indent(out) << "if((_iType >> 4) == "	<< tid << "){" 	<< endl;
			indent_up();
				_ENDVAL_NTOHL("*((_sInt32*)(_oPtr + 2))");
				indent(out) << "_iLen = *((_sInt32*)(_oPtr + 2));" 	<< endl;
				indent(out) << "if(_iLen >= 5){" << endl;
				indent_up();
					indent(out) << ELEM_VPTR() << "->len= _iLen - 5;" 	<< endl;
					indent(out) << ELEM_VPTR() << "->val= (char * )(_oPtr + 6);" << endl;
	}
					indent(out) << pFid << " |= (0X01 << " << tf->get_fid()%EDF_FID_FACTOR << ");" << endl;
					indent(out) << "return (_iLen + 2);" << endl;
				indent_down();
				indent(out) 	<< "} else {" 	<< endl;
				indent_up();
					// Add the Comment 
					string sComment = "Check OOR : ";
					sComment += tf->get_name();
					gen_comment(out, sComment);
					
					indent(out) << "if(_oStatus)  (*_oStatus) |= _EDF_E_OOR; " << endl;
					indent(out) << "return -1;" << endl;
				indent_down();
				indent(out) 	<< "}"			<< endl;
			indent_down();
			indent(out) << "} else {" 			<< endl;
			indent_up();
				indent(out) << "if(_oStatus)  (*_oStatus) |= _EDF_E_UNK; " << endl;
				indent(out) << "return " << _dec_unknown <<"(_oPtr, _oLen);" << endl;
			indent_down();
			indent(out) << "}" 					<< endl;
			indent(out) << "return -1;" 		<< endl;
	
		indent_down();
		indent(out) << "}"	<< endl 
							<< endl;
}


void	Edf_GenerC::gen_method_string(ofstream& out, Edf_Type * tt, Edf_Field * tf, bool unk)
{
	// Add the Comment 
	string sComment = "Methods of Sequence: ";
	sComment += tf->get_name();
	gen_comment(out, sComment);

	gen_exist_seq(out, tt, tf, unk);
	gen_clear_seq(out, tt, tf, unk);

	gen_get_seq(out, tt, tf, unk);
	gen_set_seq(out, tt, tf, unk);

	gen_enc_seq(out, tt, tf, unk);
	gen_size_seq(out, tt, tf, unk);
	gen_dec_seq(out, tt, tf, unk);
}


/*------------------------------------------------------------------------------------------------------------
** Generate the Method Sequence AT
*/
void	Edf_GenerC::gen_setAt_seq(ofstream& out, Edf_Type * tt, Edf_Field * tf, bool unk)
{
	ostringstream nss;
	nss 	<<	tf->get_fid()/EDF_FID_FACTOR;
	string nFid = nss.str();	
	string tn, fn, tfn; 
	fn		= tf->get_name();	
	tfn 	= get_typename(get_true_type(tf->get_type()));
	if(unk == false){
		string vFid, pFid, aFid;
		ostringstream ass;
		ass	 << "_xPtr->_presence." << fn << "[_idx/" << EDF_FID_FACTOR << "]";
		aFid =  ass.str();

		tn   = tt->get_name();
		vFid = "_xPtr->" + fn + "[_idx]";
		pFid = "_xPtr->_presence._fid[" + nFid  +  "]";

		out << "static _sInt32 _xSetAt_"  << tn << "_"	<< fn	<< "(" << tn << " * _xPtr,  _uInt32 _idx){"	<<	endl;
		indent_up();
		indent(out) << "if(_idx < " << tf->get_repeat() << "){"
					<< endl;
		indent_up();
			#if 1
			if(get_type_id(get_true_type(tt)) == EDF_T_UNION){
				indent(out) << "_xClean_" << tn << "(_xPtr);" << endl;
			}
			#endif
			indent(out) << pFid << " |= (0X01 << "	<< tf->get_fid()%EDF_FID_FACTOR << ");" 	<< endl;
			indent(out) << aFid << " |= (0X01 <<"	<< "(_idx%" << EDF_FID_FACTOR << "));"		<< endl;
			indent(out) << "return (0);" 
						<< endl;
	}
	else{
		CLUE_PREFIX();
		tn 	 = "void ";
		out << "static _sInt32 _xSetAt_"  << prefix<< "_"<<  fn	<< "(" << tn << " * _xPtr,  _uInt32 _idx){"	<<	endl;
		indent_up();
		CLUE_OFF_IDX_P();		CLUE_OFF_IDX_A();
		indent(out) << "if(_idx < " << tf->get_repeat() << "){"		<< endl;
		indent_up();
			#if 1
			if(get_type_id(get_true_type(tt)) == EDF_T_UNION){
				indent(out) << "_xClean_" << prefix << "(_xPtr);" 	<< endl;
			}
			#endif
			indent(out) << ELEM_P() << " |= (0X01 << "	<< tf->get_fid()%EDF_FID_FACTOR << ");" 			<< endl;
			indent(out) << ELEM_A()<< " |= (0X01 <<"	<< "(_idx%" << EDF_FID_FACTOR << "));"					<< endl;
			indent(out) << "return (0);"  << endl;
	}
	indent_down();
	indent(out) << "}"				<< endl;
	indent(out) << "return (-1);" 	<< endl;
	indent_down();
	out <<	"}" 
		<<	endl 
		<<	endl;
}

void	Edf_GenerC::gen_existAt_seq(ofstream& out, Edf_Type * tt, Edf_Field * tf, bool unk)
{
	ostringstream nss;
	nss 	<<	tf->get_fid()/EDF_FID_FACTOR;
	string nFid = nss.str();	
	string tn, fn, tfn; 
	fn		= tf->get_name();	
	tfn 	= get_typename(get_true_type(tf->get_type()));
	if(unk == false){
		string vFid, pFid, aFid;
		ostringstream ass;
		ass  << "_xPtr->_presence." << fn << "[_idx/" << EDF_FID_FACTOR << "]";
		aFid =	ass.str();

		tn	 = tt->get_name();
		vFid = "_xPtr->" + fn + "[_idx]";
		pFid = "_xPtr->_presence._fid[" + nFid	+  "]";

		out << "static _sInt32 _xExistAt_" << tn << "_" << fn	<< "(" << tn << " * _xPtr,	_uInt32 _idx){" << endl;
		indent_up();
		indent(out) << "if((_idx < " << tf->get_repeat() << ") && "	<< endl;
		indent(out) << "  (" << pFid << " & (0X01 << "	<< tf->get_fid()%EDF_FID_FACTOR << "))){" 			<< endl;
		indent_up();
			indent(out) << "if(" << aFid <<  " & (0X01 <<"	<< "(_idx%" << EDF_FID_FACTOR << "))){"			<< endl;
	}
	else{
		CLUE_PREFIX();
		tn	 = "void ";

		out << "static _sInt32 _xExistAt_" << prefix<< "_"<<fn	<< "(" << tn << " * _xPtr,	_uInt32 _idx){" << endl;
		indent_up();
		CLUE_OFF_IDX_P();
		CLUE_OFF_IDX_A();
		indent(out) << "if((_idx < " << tf->get_repeat() << ") && "	<< endl;
		indent(out) << "  (" << ELEM_P() << " & (0X01 << "	<< tf->get_fid()%EDF_FID_FACTOR << "))){" 		<< endl;
		indent_up();
			indent(out) << "if(" << ELEM_A() <<  " & (0X01 <<"	<< "(_idx%" << EDF_FID_FACTOR << "))){"		<< endl;
	}
		indent_up();
		indent(out) << "return (1);"	<< endl;
		indent_down();
		indent(out) << "}" 				<< endl;
	indent_down();
	indent(out) << "}" 					<< endl;
	indent(out) << "return (0);" 		<< endl;
	indent_down();
	out <<	"}" 	
		<<	endl 
		<<	endl;
}

void	Edf_GenerC::gen_clearAt_seq(ofstream& out, Edf_Type * tt, Edf_Field * tf, bool unk)
{
	ostringstream nss;
	nss 	<<	tf->get_fid()/EDF_FID_FACTOR;
	string nFid = nss.str();	
	string tn, fn, tfn; 
	fn		= tf->get_name();	
	tfn 	= get_typename(get_true_type(tf->get_type()));
	if(unk == false){
		string vFid, pFid, aFid;
		ostringstream ass;
		ass  << "_xPtr->_presence." << fn << "[_idx/" << EDF_FID_FACTOR << "]";
		aFid =	ass.str();

		tn	 = tt->get_name();
		vFid = "_xPtr->" + fn + "[_idx]";
		pFid = "_xPtr->_presence._fid[" + nFid	+  "]";

		out << "static void _xClearAt_" << tn << "_" << fn	<< "(" << tn << " * _xPtr,	_uInt32 _idx){" << endl;
		indent_up();
		indent(out) << "if((_idx < " << tf->get_repeat() << ") && "	<< endl;
		indent(out) << "  (" << pFid << " & (0X01 << "	<< tf->get_fid()%EDF_FID_FACTOR << "))){" 			<< endl;
		indent_up();
			indent(out) << aFid <<  " &= ~(0X01 <<"	<< "(_idx%" << EDF_FID_FACTOR << "));"			<< endl;
	}
	else{
		CLUE_PREFIX();
		tn	 = "void ";

		out << "static void _xClearAt_" << prefix<< "_"<<fn	<< "(" << tn << " * _xPtr,	_uInt32 _idx){" << endl;
		indent_up();
		CLUE_OFF_IDX_P();
		CLUE_OFF_IDX_A();
		indent(out) << "if((_idx < " << tf->get_repeat() << ") && "	<< endl;
		indent(out) << "  (" << ELEM_P() << " & (0X01 << "	<< tf->get_fid()%EDF_FID_FACTOR << "))){" 		<< endl;
		indent_up();
			indent(out) << ELEM_A() <<  " &= ~(0X01 <<"	<< "(_idx%" << EDF_FID_FACTOR << "));"		<< endl;
	}
	indent_down();
	indent(out) << "}" 					<< endl;
	indent(out) << "return;" 		<< endl;
	indent_down();
	out <<	"}" 	
		<<	endl 
		<<	endl;
}

void	Edf_GenerC::gen_getAt_seq(ofstream& out, Edf_Type * tt, Edf_Field * tf, bool unk)
{
	ostringstream nss;
	nss 	<<	tf->get_fid()/EDF_FID_FACTOR;
	string nFid = nss.str();	
	string tn, fn, tfn; 
	fn		= tf->get_name();	
	tfn 	= get_typename(get_true_type(tf->get_type()));
	if(tfn == ""){
		tfn = "void ";
	}
	if(unk == false){
		string vFid, pFid, aFid;
		ostringstream ass;
		ass  << "_xPtr->_presence." << fn << "[_idx/" << EDF_FID_FACTOR << "]";
		aFid =	ass.str();

		tn	 = tt->get_name();
		vFid = "_xPtr->" + fn + "[_idx]";
		pFid = "_xPtr->_presence._fid[" + nFid	+  "]";

		out << "static " 	<< tfn << "*_xGetAt_"  << tn << "_" 	<< fn	<< "(" 	<< tn << " * _xPtr,	_uInt32 _idx){" <<	endl;
		indent_up();
		indent(out) << "if(_idx < " << tf->get_repeat() << "){"	<< endl;
		indent_up();
		indent(out) << "return &(" << vFid << ");"				<< endl;
	}
	else{
		CLUE_PREFIX();
		tn	 = "void ";
		out << "static " 	<< tfn << "*_xGetAt_"  << prefix << "_"	<<	fn	<< "(" 	<< tn << " * _xPtr,	_uInt32 _idx){" <<	endl;
		indent_up();
		CLUE_OFF_IDX_V();
		indent(out) << "if(_idx < " << tf->get_repeat() << "){"	<< endl;
		indent_up();
		indent(out) << "return " << ELEM_VPTR() << ";"			<< endl;
	}
	indent_down();
	indent(out) << "}" 				<< endl;
	indent(out) << "return (NULL);" << endl;
	indent_down();
	out <<	"}" 	
		<<	endl 
		<<	endl;	
}

void	Edf_GenerC::gen_decAt_seq(ofstream& out, Edf_Type * tt, Edf_Field * tf, bool unk)
{
	string sComment;
	ostringstream nss;
	nss 	<<	tf->get_fid()/EDF_FID_FACTOR;
	string 	nFid = nss.str();	
	string 	tn, fn, tfn, pFid, aFid; 	
	Edf_Type * 	ttf;
	int 		tid;
	
	ttf 	= get_true_type(tf->get_type());
	tid 	= get_type_id(ttf);
	fn		= tf->get_name();	
	tfn 	= get_typename(get_true_type(tf->get_type()));
	
	if(unk == false){
		ostringstream ass; 
		string vFid;
		tn	 = tt->get_name();
		ass  << "_xPtr->_presence." << fn;
		aFid =	ass.str();
		tn	 = tt->get_name();
		vFid = "_xPtr->" + fn;
		pFid = "_xPtr->_presence._fid[" + nFid	+  "]";

		out << "static _sInt32 _xDecAt_"	<< tn	<< "_"	<< fn	<< "(" << tn	<< " * _xPtr, _uInt8 * _oPtr, _sInt32 _oLen, _sInt32 * _oStatus){"	<<	endl;
		indent_up();
			indent(out) << "_uInt32 _jdx, _idx, _iCnt, _mCnt, _mask, _sum;" 	<< endl;
			indent(out) << "_uInt32 *_lPtr, *_hPtr, *_aPtr;" 	<< endl;
			indent(out) << tfn << " *_sPtr, *_vPtr, *_cPtr;" 	<< endl;

			indent(out) << "_sPtr = ("	<< tfn	<< "*)(" << vFid <<");" 			<< endl; 

	}
	else{
		CLUE_PREFIX();
		tn	 = "void ";
		out << "static _sInt32 _xDecAt_"	<< prefix<< "_"	<< fn	<< "(" << tn	<< " * _xPtr, _uInt8 * _oPtr, _sInt32 _oLen, _sInt32 * _oStatus){"	<<	endl;
		indent_up();
			indent(out) << "_uInt32 _jdx, _idx, _iCnt, _mCnt, _mask, _sum;" 	<< endl;
			indent(out) << "_uInt32 *_lPtr, *_hPtr, *_aPtr;" 	<< endl;
			indent(out) << tfn << " *_sPtr, *_vPtr, *_cPtr;" 	<< endl;

			CLUE_OFF_IDX_VBEG();	CLUE_OFF_IDX_ABEG();	CLUE_OFF_IDX_P();
			pFid = ELEM_P() ; aFid = ELEM_APTR();
			
			indent(out) << "_sPtr = "	<<	ELEM_VPTR() <<";"					<< endl;
	}
			// Add the Comment 
			sComment = "Check Type : ";
			sComment += tf->get_name();
			gen_comment(out, sComment);

			indent(out) << "_mask = *(_oPtr + 1);" 	<< endl;
			indent(out) << "if((_mask >> 4) == "	<< tid << "){" << endl;
			indent_up();
				/* Judge the Count */
				_ENDVAL_NTOHL("*((_uInt32 *)(_oPtr + 2))");
				_ENDVAL_NTOHL("*((_uInt32 *)(_oPtr + 6))");
				_ENDVAL_NTOHL("*((_uInt32 *)(_oPtr + 10))");
				indent(out) << "_sum  = *((_uInt32*)(_oPtr + 2));" 			<< endl;
				indent(out) << "_iCnt = *((_uInt32*)(_oPtr + 6));" 			<< endl;
				indent(out) << "_mCnt = *((_uInt32*)(_oPtr + 10));" 		<< endl;
				
				indent(out) << "_lPtr = (_uInt32 *)(_oPtr + _sum - (_iCnt*4) - (_mCnt*8));" << endl;
				indent(out) << "_hPtr = (_uInt32 *)(_lPtr + _iCnt);" << endl;

				indent(out) << "if((_iCnt > 0) && (_iCnt <= " << tf->get_repeat() << ")){" << endl;
				indent_up();
					
					// Add the Comment 
					sComment = "Presence Of A: ";
					sComment += tf->get_name();
					gen_comment(out, sComment);					
					indent(out) << "_aPtr = "	<< aFid <<";"	<< endl; 

					indent(out) << "for(_jdx = 0, _sum = 14; _jdx < _mCnt; _jdx++, _hPtr+=2){" << endl;
					indent_up();
						_ENDVAL_NTOHL("*(_hPtr)");
						_ENDVAL_NTOHL("*(_hPtr + 1)");
						indent(out) << "_mask = *(_hPtr + 1);  _aPtr[*_hPtr] = _mask; _vPtr = _sPtr + (*_hPtr) * 32;" << endl;
						
						indent(out) << "for(_idx = 0; _idx < 32; _idx++, _mask >>= 1){"	<< endl;
						indent_up();
							indent(out) << "if(_mask == 0){" << endl;
							indent_up();
								indent(out) << "break;" << endl;
							indent_down();
							indent(out) << "}" << endl;
							indent(out) << "if((_mask & 0X01) && (_iCnt > 0)){"	<< endl; 
							indent_up();
								indent(out) << " _cPtr = (_vPtr + _idx); " 							<< endl;
								_ENDVAL_NTOHL("*(_lPtr)");
								indent(out) << "_cPtr->val = (char *)(_oPtr + _sum);"				<< endl;
								indent(out) << "_cPtr->len = (_sInt32)((*_lPtr) - _sum) -1;"		<< endl; 
								indent(out) << "_sum = *_lPtr; 	_lPtr += 1; _iCnt -=1; " 			<< endl;
								
							indent_down();
							indent(out) << "}"			<< endl;
						indent_down();
						indent(out) << "}"			<< endl;
					indent_down();
					indent(out) << "}"			<< endl;
					
					indent(out) << pFid << " |= (0X01 << " << tf->get_fid()%EDF_FID_FACTOR << ");" 	<< endl;
					indent(out) << "return *((_sInt32*)(_oPtr + 2));" << endl;
					indent_down();
				indent(out) << "} else {" << endl;
				indent_up();
					indent(out) << "if(_oStatus)  (*_oStatus) |= (_EDF_E_CRT | _EDF_E_OOR); " << endl;
					indent(out) << "return -1;" << endl;
				indent_down();
				indent(out) << "}" << endl;
					
			indent_down();
			indent(out) << "} else {" << endl;
			indent_up();
				indent(out) << "if(_oStatus)  (*_oStatus) |= _EDF_E_UNK; " << endl;
				indent(out) << "return " << _dec_unknown <<"(_oPtr, _oLen);" << endl;
			indent_down();
			indent(out) << "}" << endl;
		
			indent(out) << "return -1;" << endl;
		indent_down();
		indent(out) << "}"	<< endl 
							<< endl;
}

void	Edf_GenerC::gen_encAt_seq(ofstream& out, Edf_Type * tt, Edf_Field * tf, bool unk)
{
	string sComment;
	ostringstream nss;	
	string tn, fn, tfn; 
	Edf_Type *	ttf;
	ttf 	= get_true_type(tf->get_type());
	fn		= tf->get_name();	
	tfn 	= get_typename(get_true_type(tf->get_type()));
	int  repeat = (tf->get_repeat() + EDF_FID_FACTOR - 1)/EDF_FID_FACTOR;

	if(unk == false){
		ostringstream ass; 
		string vFid, aFid;
		tn	 = tt->get_name();
		ass  << "_xPtr->_presence." << fn;
		vFid = "_xPtr->" + fn;
		aFid =	ass.str();

		out << "static _sInt32 _xEncAt_"	<< tn	<< "_"	<< fn	<< "(" << tn	<< " * _xPtr, _uInt8 * _oPtr, _sInt32 _oLen, _sInt32 * _oStatus){" <<	endl;
		indent_up();
			indent(out) << "_uInt32 _idx, _jdx, _iCnt, _mCnt, _mask;"	<< endl;
			indent(out) << tfn << "*_vPtr, *_sPtr;" 					<< endl;
			indent(out) << "_uInt32 *_hPtr, *_aPtr;"					<< endl;
			indent(out) << "_uInt8  *_iPtr;" 							<< endl;
			indent(out) << "_uInt32 _aTmp[" 	<<  repeat*2			<< "];" << endl;
			indent(out) << "_uInt32 _lTmp[" 	<<  tf->get_repeat()	<< "];" << endl;

			indent(out) << "_sPtr = ("	<< tfn	<< "*)(" << vFid <<");"	<< endl; 
			indent(out) << "_aPtr = "	<< aFid <<";"					<< endl; 
	}
	else{
		CLUE_PREFIX();
		tn	 = "void ";
		out << "static _sInt32 _xEncAt_"<< prefix	 << "_"	<< fn	<< "(" << tn	<< " * _xPtr, _uInt8 * _oPtr, _sInt32 _oLen, _sInt32 * _oStatus){"	<<	endl;
		indent_up();
			indent(out) << "_uInt32 _idx, _jdx, _iCnt, _mCnt, _mask;"	<< endl;
			indent(out) << tfn << "*_vPtr, *_sPtr;" 					<< endl;
			indent(out) << "_uInt32 *_hPtr, *_aPtr;"					<< endl;
			indent(out) << "_uInt8  *_iPtr;" 							<< endl;
			indent(out) << "_uInt32 _aTmp[" 	<<  repeat*2			<< "];" << endl;
			indent(out) << "_uInt32 _lTmp[" 	<<  tf->get_repeat()	<< "];" << endl;

			CLUE_OFF_IDX_VBEG();	 
			CLUE_OFF_IDX_ABEG();
			indent(out) << "_sPtr = "	<<	ELEM_VPTR() <<";"		<< endl;
			indent(out) << "_aPtr = "	<<	ELEM_APTR() <<";"		<< endl;
	}
		// Add the Comment 
		sComment = "Judge Length : ";
		sComment += tf->get_name();
		gen_comment(out, sComment);
		
		indent(out) << "if( EDF_PACK_MIN <= _oLen){"		<< endl;
		indent_up();
			indent(out) << "*_oPtr = "			<< (tf->get_fid()<< 0X01)	<< " | (0X01);" << endl; 
			indent(out) << "*(_oPtr + 1) = "	<< (get_type_id(ttf)<< 0X04)<< ";"	<< endl; 
			indent(out) << "_hPtr = _aTmp;"		<< endl; 
			indent(out) << "_iPtr = (_uInt8 *) (_oPtr + 14);" 	<< endl;
			indent(out) << "_oLen  -= 14;"		<< endl;

			indent(out) << "for(_jdx = 0, _iCnt = 0, _mCnt = 0; _jdx < " << repeat << "; _jdx++, _aPtr++, _sPtr+=32){" << endl;
			indent_up();
				indent(out) << "if((*_aPtr) != 0){" << endl;
				indent_up();
					indent(out) << "*_hPtr = _jdx; *(_hPtr + 1) = *_aPtr;  " << endl;
					_ENDVAL_HTONL("*_hPtr");
					_ENDVAL_HTONL("*(_hPtr + 1)");
					indent(out) << "_hPtr += 2; _mCnt += 1; _oLen -= 8;"	<< endl;
						
					indent(out) << "for(_idx = 0, _mask =(*_aPtr); _idx < 32; _idx++, _mask >>= 1){"	<< endl;
					indent_up();
						indent(out) << "if(_mask == 0){" << endl;
						indent_up();
							indent(out) << "break;" << endl;
						indent_down();
						indent(out) << "}" << endl;

						indent(out) << "if(_mask & 0X01){"	<< endl; 
						indent_up();
							indent(out) << "_vPtr = _sPtr + _idx;" << endl;
							indent(out) << "_oLen-= (_vPtr->len + 5);" << endl;
							
							indent(out)	<< "if(_oLen > 0){"	<< endl; 
							indent_up();
								indent(out) << "_xEdfCopy(_iPtr, _vPtr->val, _vPtr->len);"				<< endl;
								indent(out) << "_iPtr += _vPtr->len; 	*(_iPtr) = 0; 	_iPtr += 1;"	<< endl;
								indent(out) << "_lTmp[_iCnt] = (_uInt32)(_iPtr - _oPtr);"				<< endl;
								_ENDVAL_HTONL("_lTmp[_iCnt]");
								indent(out) << "_iCnt += 1;" 				<< endl;
							indent_down();
							indent(out) << "} else {"		<< endl;
							indent_up();
								// Add the Comment 
								sComment = "Check OOM : ";
								sComment += tf->get_name();
								gen_comment(out, sComment);
							
								indent(out) << "if(_oStatus)  (*_oStatus) |= _EDF_E_OOM; " << endl;
								indent(out) << "return 0;" << endl;
							indent_down();
							indent(out) << "}"	<< endl;
						indent_down();
						indent(out) << "}" 	<< endl;
					indent_down();
					indent(out) << "}"		<< endl;
				indent_down();
				indent(out) << "}"		<< endl;
			indent_down();
			indent(out) << "}"		<< endl;

			indent(out) << "if( _iCnt > 0){" << endl;
			indent_up();
				indent(out) << "_hPtr = (_uInt32 *)_iPtr;"	<< endl; 
				indent(out) << "_xEdfCopy(_hPtr, _lTmp, _iCnt * 4); _hPtr += _iCnt;"		<< endl;
				indent(out) << "_xEdfCopy(_hPtr, _aTmp, _mCnt * 8); _hPtr += (_mCnt * 2);"	<< endl;
				indent(out) << "_jdx  = (_uInt32)((_uInt8*)_hPtr - (_uInt8*)_oPtr);" 		<< endl;
				indent(out) << "*((_uInt32 *)(_oPtr + 2))  = _jdx;" << endl;
				indent(out) << "*((_uInt32 *)(_oPtr + 6))  = _iCnt;" << endl;
				indent(out) << "*((_uInt32 *)(_oPtr + 10)) = _mCnt;" << endl;
				_ENDVAL_HTONL("*((_uInt32 *)(_oPtr + 2))");
				_ENDVAL_HTONL("*((_uInt32 *)(_oPtr + 6))");
				_ENDVAL_HTONL("*((_uInt32 *)(_oPtr + 10))");
				
				indent(out) << "return _jdx;" << endl;
			indent_down();
			indent(out) << "}"		<< endl;

		indent_down();
		indent(out) << "}"			<< endl;
		
	indent(out) << "return (0);"	<< endl;
	indent_down();	
	out <<	"}"		
		<<	endl	
		<<	endl;	
}

void	Edf_GenerC::gen_sizeAt_seq(ofstream& out, Edf_Type * tt, Edf_Field * tf, bool unk)
{
	ostringstream nss;	
	string tn, fn, tfn; 
	Edf_Type *	ttf;
	ttf 	= get_true_type(tf->get_type());
	fn		= tf->get_name();	
	tfn 	= get_typename(get_true_type(tf->get_type()));
	if(unk == false){
		ostringstream ass; 
		string vFid, aFid;
		tn	 = tt->get_name();
		ass  << "_xPtr->_presence." << fn;
		vFid = "_xPtr->" + fn;
		aFid =	ass.str();

		out << "static _sInt32 _xSizeAt_"	<< tn	<< "_"	
			<< fn	<< "(" << tn	<< " * _xPtr){" <<	endl;
		indent_up();
			indent(out) << "_uInt32 _idx, _jdx, _mCnt, _mask, _sum;"			<< endl;
			indent(out) << tfn << "*_vPtr, *_iPtr;" 			<< endl;
			indent(out) << "_uInt32 *_aPtr;"					<< endl;
			indent(out) << "_vPtr = ("	<< tfn	<< "*)(" << vFid <<");"	<< endl; 
			indent(out) << "_aPtr = "	<< aFid <<";"					<< endl; 
	}
	else{
		CLUE_PREFIX();
		tn	 = "void ";
		out << "static _sInt32 _xSizeAt_"<< prefix	 << "_"	
			<< fn	<< "(" << tn	<< " * _xPtr){"	<<	endl;
		indent_up();
			indent(out) << "_uInt32 _idx, _jdx, _mCnt, _mask, _sum;"		<< endl;
			indent(out) << tfn << "*_vPtr, *_iPtr;" 			<< endl;
			indent(out) << "_uInt32 *_aPtr;"					<< endl;
			CLUE_OFF_IDX_VBEG();	 
			CLUE_OFF_IDX_ABEG();
			indent(out) << "_vPtr = "	<<	ELEM_VPTR() <<";"		<< endl;
			indent(out) << "_aPtr = "	<<	ELEM_APTR() <<";"		<< endl;
	}

		int  repeat = (tf->get_repeat() + EDF_FID_FACTOR - 1)/EDF_FID_FACTOR;
		indent(out) << "for(_jdx = 0, _mCnt = 0, _sum = 0; _jdx < " << repeat << "; _jdx++, _aPtr++, _vPtr+=32){" << endl;
		indent_up();
			indent(out) << "if((*_aPtr) != 0){" << endl;
			indent_up();
				indent(out) << "_mCnt += 1;" 	<< endl;
				indent(out) << "for(_idx = 0, _mask = (*_aPtr); _idx < 32; _idx++, _mask >>= 1){" << endl;
				indent_up();
					indent(out) << "if(_mask == 0){" << endl;
					indent_up();
						indent(out) << "break;" << endl;
					indent_down();
					indent(out) << "}" << endl;
					indent(out) << "if(_mask & 0X01){"	<< endl; 
					indent_up();
						indent(out) << "_iPtr = _vPtr + _idx;" 			<< endl;
						indent(out) << "_sum += (_iPtr->len + 5);" 		<< endl; 
					indent_down();
					indent(out) << "}" 	<< endl;
				indent_down();
				indent(out) << "}"		<< endl;
			indent_down();
			indent(out) << "}"		<< endl;
		indent_down();
		indent(out) << "}"		<< endl;

		indent(out) << "if( _mCnt > 0){" << endl;
		indent_up();
			indent(out) << "return (_sum + 14 + (_mCnt * 8));" 	<< endl;
		indent_down();
		indent(out) << "}"		<< endl;
		
	indent(out) << "return (0);"		<< endl;
	indent_down();	
	out <<	"}"		
		<<	endl	
		<<	endl;	
}


void	Edf_GenerC::gen_count_seq(ofstream& out, Edf_Type * tt, Edf_Field * tf, bool unk)
{
	ostringstream nss;	
	string tn, fn, tfn; 
	Edf_Type *	ttf;
	ttf 	= get_true_type(tf->get_type());
	fn		= tf->get_name();	
	tfn 	= get_typename(get_true_type(tf->get_type()));
	if(unk == false){
		ostringstream ass; 
		string vFid, aFid;
		tn	 = tt->get_name();
		ass  << "_xPtr->_presence." << fn;
		vFid = "_xPtr->" + fn;
		aFid =	ass.str();

		out << "static _sInt32 _xCount_"	<< tn	<< "_"	
			<< fn	<< "(" << tn	<< " * _xPtr){" <<	endl;
		indent_up();
			indent(out) << "_sInt32 _idx, _jdx, _cnt;"			<< endl;
			indent(out) << "_uInt32 _mask, *_aPtr;"			<< endl << endl;
			indent(out) << "_aPtr = "	<< aFid <<";"					<< endl; 
	}
	else{
		CLUE_PREFIX();
		tn	 = "void ";
		out << "static _sInt32 _xCount_"<< prefix	 << "_"	
			<< fn	<< "(" << tn	<< " * _xPtr){"	<<	endl;
		indent_up();
			indent(out) << "_sInt32 _idx, _jdx, _cnt;"			<< endl;
			indent(out) << "_uInt32 _mask, *_aPtr;"			<< endl << endl;
			CLUE_OFF_IDX_ABEG();
			indent(out) << "_aPtr = "	<<	ELEM_APTR() <<";"		<< endl;
	}

		int  repeat = (tf->get_repeat() + EDF_FID_FACTOR - 1)/EDF_FID_FACTOR;
		indent(out) << "for(_jdx = 0, _cnt = 0; _jdx < " << repeat << "; _jdx++, _aPtr++){" << endl;
		indent_up();
			indent(out) << "if((*_aPtr) != 0){" << endl;
			indent_up();
				indent(out) << "for(_idx = 0, _mask = (*_aPtr); _idx < 32; _idx++, _mask >>= 1){" << endl;
				indent_up();
					indent(out) << "if(_mask == 0){" << endl;
					indent_up();
						indent(out) << "break;" << endl;
					indent_down();
					indent(out) << "}" << endl;
					indent(out) << "if(_mask & 0X01){"	<< endl; 
					indent_up();
						indent(out) << "_cnt += 1;" 				<< endl;
					indent_down();
					indent(out) << "}" 	<< endl;
				indent_down();
				indent(out) << "}"		<< endl;
			indent_down();
			indent(out) << "}"		<< endl;
		indent_down();
		indent(out) << "}"		<< endl;

	indent(out) << "return (_cnt);"		<< endl;
	indent_down();	
	out <<	"}"		
		<<	endl	
		<<	endl;	
}

void	Edf_GenerC::gen_methodAt_string(ofstream& out, Edf_Type * tt, Edf_Field * tf, bool unk)
{
	// Add the Comment 
	string sComment = "Methods of Sequence[]: ";
	sComment += tf->get_name();
	gen_comment(out, sComment);

	gen_existAt_seq(out, tt, tf, unk);
	gen_clearAt_seq(out, tt, tf, unk);
	
	gen_getAt_seq(out, tt, tf, unk);
	gen_setAt_seq(out, tt, tf, unk);

	gen_encAt_seq(out, tt, tf, unk);
	gen_sizeAt_seq(out, tt, tf, unk);
	gen_count_seq(out, tt, tf, unk);
	gen_decAt_seq(out, tt, tf, unk);
}


/* ------------------------------------------------------------------
**	About XML / Parse
** 	Using the Tiny Xml
*/
static 	int 	Edf_XmlRd_Iter(EdfXml& vXml, TiXmlElement * pElem, Edf_Prog * eProg, Edf_Pack * ePack);

static 	int 	Edf_XmlRd_Enum(EdfXml& vXml, TiXmlElement * pElem, Edf_Prog * eProg)
{
	string 			sId;
	Edf_Enum 	* 	eEnum;
	
	TiXmlElement * 	pChild;

	/* Attr  */
	sId   = pElem->ValueStr();
	eEnum = new Edf_Enum(eProg, sId);

	/* Let's Check the Constants */
	pChild = vXml.GetChild(pElem);
	if(pChild){
		TiXmlElement *	pNext;
		const string *	sVal;
		string 			nVal;
		Edf_EnumVal	*	eValue;
		int 			rValue, vValue;
		
		pElem = pChild;
		rValue = 0;
		while(pElem){
			pNext = vXml.GetNext(pElem);
			/* The Values */
			nVal = pElem->ValueStr();
			sVal = vXml.GetAttr(pElem, string("value"), &vValue);
			if(sVal == NULL){
				eValue = new Edf_EnumVal(nVal, rValue);
				rValue += 1;
			}
			else{
				eValue = new Edf_EnumVal(nVal, vValue);
				rValue = (vValue + 1);
			}
			eEnum->append(eValue);
			pElem = pNext;
		}
		
	}
	else{
		return -1;
	}
	
	eProg->add_enum(eEnum);
	return 0;
}

static 	int 	Edf_XmlRd_Struct(EdfXml& vXml, TiXmlElement * pElem, Edf_Prog * eProg, Edf_Pack * ePack)
{
	Edf_Pack    *   eSubPack;
	Edf_Struct	* 	eStruct;

	string 			sId;
	const string*	sAttr;
	int 			vFid;
	int 			vRepeat;
	Edf_Field	*	eField;
	
	TiXmlElement * 	pChild;

	/* Attr  */
	sId   = pElem->ValueStr();
	vFid  = 0;
	sAttr = vXml.GetAttr(pElem, string("fid"), &vFid);
	if(sAttr == NULL){
		/* It's a Typedef */
		eStruct = new Edf_Struct(eProg, sId);
		eSubPack = (Edf_Pack *)eStruct;
		eProg->add_pack(eSubPack);

	}
	else{
		/* It's an Annomy */
		if(!ePack)
			return -1;

		if((vFid > 0) && (vFid < 128)){
			eStruct = new Edf_Struct(eProg);
			eSubPack= (Edf_Pack *)eStruct;

			vRepeat = 1;
			sAttr = vXml.GetAttr(pElem, string("repeat"), &vRepeat);
			if(vRepeat < 1){
				vRepeat = 1;
			}
			eField  = new Edf_Field(eStruct, sId, vFid, vRepeat);
			ePack->append(eField);
		}
		else{
			return -1;
		}
		
	}
	
	/* Let's Check the Child */
	pChild = vXml.GetChild(pElem);
	if(pChild){
		Edf_XmlRd_Iter(vXml, pChild, eProg, eSubPack);
	}
	
	return 0;
}

static 	int 	Edf_XmlRd_Union(EdfXml& vXml, TiXmlElement * pElem, Edf_Prog * eProg, Edf_Pack * ePack)
{
	Edf_Pack    *   eSubPack;
	Edf_Union	* 	eUnion;

	string 			sId;
	const string*	sAttr;
	int 			vFid;
	int 			vRepeat;
	Edf_Field	*	eField;
	
	TiXmlElement * 	pChild;

	/* Attr  */
	sId   = pElem->ValueStr();
	vFid  = 0;
	sAttr = vXml.GetAttr(pElem, string("fid"), &vFid);
	if(sAttr == NULL){
		/* It's a Typedef */
		eUnion = new Edf_Union(eProg, sId);
		eSubPack = (Edf_Pack *)eUnion;
		eProg->add_pack(eSubPack);

	}
	else{
		/* It's an Annomy */
		if(!ePack)
			return -1;

		if((vFid > 0) && (vFid < 128)){
			eUnion = new Edf_Union(eProg);
			eSubPack= (Edf_Pack *)eUnion;

			vRepeat = 1;
			sAttr = vXml.GetAttr(pElem, string("repeat"), &vRepeat);
			if(vRepeat < 1){
				vRepeat = 1;
			}
			eField  = new Edf_Field(eUnion, sId, vFid, vRepeat);
			ePack->append(eField);
		}
		else{
			return -1;
		}
		
	}
	
	/* Let's Check the Child */
	pChild = vXml.GetChild(pElem);
	if(pChild){
		Edf_XmlRd_Iter(vXml, pChild, eProg, eSubPack);
	}
	
	return 0;
}

static 	int 	Edf_XmlRd_Basic(EdfXml& vXml, TiXmlElement * pElem, Edf_Prog * eProg, Edf_Pack * ePack)
{
	const string * 	sType;
	const string*	sAttr;
	string 			sId;
	int 			vFid;
	int 			vRepeat;
	Edf_Field	*	eField;
	Edf_Basic	*	eBasic;
	
	/* Attr  */
	sId   = pElem->ValueStr();
	vFid  = 0;
	sAttr = vXml.GetAttr(pElem, string("fid"), &vFid);
	if((sAttr == NULL) || (!ePack)){
		return -1;
	}

	vRepeat = 1;
	sAttr = vXml.GetAttr(pElem, string("repeat"), &vRepeat);
	if(vRepeat < 1){
		vRepeat = 1;
	}

	sType = vXml.GetAttr(pElem, string("type"));
	string cType = *sType;
	if(cType == "_sInt8"){
			eBasic = new Edf_Basic(TYPE_S8);
			eField = new Edf_Field(eBasic, sId, vFid, vRepeat);
			ePack->append(eField);
	}
	else if(cType == "_uInt8"){
			eBasic = new Edf_Basic(TYPE_U8);
			eField = new Edf_Field(eBasic, sId, vFid, vRepeat);
			ePack->append(eField);
	}
	else if(cType == "_sInt16"){
			eBasic = new Edf_Basic(TYPE_S16);
			eField = new Edf_Field(eBasic, sId, vFid, vRepeat);
			ePack->append(eField);
	}
	else if(cType == "_uInt16"){
			eBasic = new Edf_Basic(TYPE_U16);
			eField = new Edf_Field(eBasic, sId, vFid, vRepeat);
			ePack->append(eField);
	}
	else if(cType == "_sInt32"){
			eBasic = new Edf_Basic(TYPE_S32);
			eField = new Edf_Field(eBasic, sId, vFid, vRepeat);
			ePack->append(eField);
	}
	else if(cType == "_uInt32"){
			eBasic = new Edf_Basic(TYPE_U32);
			eField = new Edf_Field(eBasic, sId, vFid, vRepeat);
			ePack->append(eField);
	}
	else if(cType == "_sInt64"){
			eBasic = new Edf_Basic(TYPE_S64);
			eField = new Edf_Field(eBasic, sId, vFid, vRepeat);
			ePack->append(eField);
	}
	else if(cType == "_uInt64"){
			eBasic = new Edf_Basic(TYPE_U64);
			eField = new Edf_Field(eBasic, sId, vFid, vRepeat);
			ePack->append(eField);
	}
	else if(cType == "_dFloat"){
			eBasic = new Edf_Basic(TYPE_DOUBLE);
			eField = new Edf_Field(eBasic, sId, vFid, vRepeat);
			ePack->append(eField);
	}
	else if(cType == "_cSeq"){
			eBasic = new Edf_Basic(TYPE_STRING);
			eField = new Edf_Field(eBasic, sId, vFid, vRepeat);
			ePack->append(eField);
	}
	
	return 0;
}

static 	int 	Edf_XmlRd_UType(EdfXml& vXml, TiXmlElement * pElem, Edf_Prog * eProg, Edf_Pack * ePack)
{
	const string * 	sType;
	string 			sId;
	const string *	sAttr;
	int 			vFid;
	int 			vRepeat;
	Edf_Field	*	eField;
	/* Attr  */
	sId   = pElem->ValueStr();
	vFid  = 0;
	sAttr = vXml.GetAttr(pElem, string("fid"), &vFid);
	if((sAttr == NULL) || (!ePack)){
		return -1;
	}

	vRepeat = 1;
	sAttr = vXml.GetAttr(pElem, string("repeat"), &vRepeat);
	if(vRepeat < 1){
		vRepeat = 1;
	}

	sType = vXml.GetAttr(pElem, string("type"));
	string cType = *sType;
	Edf_Type * 	eType = eProg->TypeLookup(cType);
	Edf_Pack *	eUnk;
	if(eType == NULL){
		eUnk 	= new Edf_Pack(eProg, cType);
		eType   = (Edf_Type *)eUnk;
	}
	eField	= new Edf_Field(eType, sId, vFid, vRepeat);
	ePack->append(eField);
	
	return 0;
}

static 	int 	Edf_XmlRd_Iter(EdfXml& vXml, TiXmlElement * pElem, Edf_Prog * eProg, Edf_Pack * ePack)
{
	TiXmlElement *  pNext;
	const string * 	sType;
	
	while(pElem){
		pNext = vXml.GetNext(pElem);
		sType = vXml.GetAttr(pElem, string("type"));
		string cType = *sType;

		if(cType == "struct"){
			Edf_XmlRd_Struct(vXml, pElem, eProg, ePack);
		}
		else if(cType == "union"){
			Edf_XmlRd_Union(vXml, pElem, eProg, ePack);
		}
		else if(cType == "enum"){
			Edf_XmlRd_Enum(vXml, pElem, eProg);
		}
		else if(cType == "_sInt8"){
			Edf_XmlRd_Basic(vXml, pElem, eProg, ePack);
		}
		else if(cType == "_uInt8"){
			Edf_XmlRd_Basic(vXml, pElem, eProg, ePack);
		}
		else if(cType == "_sInt16"){
			Edf_XmlRd_Basic(vXml, pElem, eProg, ePack);
		}
		else if(cType == "_uInt16"){
			Edf_XmlRd_Basic(vXml, pElem, eProg, ePack);
		}
		else if(cType == "_sInt32"){
			Edf_XmlRd_Basic(vXml, pElem, eProg, ePack);
		}
		else if(cType == "_uInt32"){
			Edf_XmlRd_Basic(vXml, pElem, eProg, ePack);
		}
		else if(cType == "_sInt64"){
			Edf_XmlRd_Basic(vXml, pElem, eProg, ePack);
		}
		else if(cType == "_uInt64"){
			Edf_XmlRd_Basic(vXml, pElem, eProg, ePack);
		}
		else if(cType == "_dFloat"){
			Edf_XmlRd_Basic(vXml, pElem, eProg, ePack);
		}
		else if(cType == "_cSeq"){
			Edf_XmlRd_Basic(vXml, pElem, eProg, ePack);
		}
		else{
			Edf_XmlRd_UType(vXml, pElem, eProg, ePack);
		}
		pElem = pNext;
	}

	return 0;
}

static 	int		EdfProg_Gen(Edf_Prog* prog, Edf_GenerC* eGener)		{
	Edf_Pack * ePack;
	Edf_Enum * eEnum;

	std::vector<Edf_Enum*>::const_iterator e_iter;
	for (e_iter = prog->_enums.begin(); e_iter != prog->_enums.end(); ++e_iter) {
		if ((*e_iter)->is_enum()) {
			eEnum = (*e_iter);
			eGener->gen_enum(eEnum);
		}
	}
	
	std::vector<Edf_Pack*>::const_iterator p_iter;
	for (p_iter = prog->_packs.begin(); p_iter != prog->_packs.end(); ++p_iter) {
		if ((*p_iter)->is_struct() || (*p_iter)->is_union()) {
			ePack = (*p_iter);
			eGener->gen_pack(ePack);
		}
	}
	return true;
}


static int 	EdfXml_Parse(string& sFile)
{
	TiXmlElement * 	pElem;

	/* Load File */
	EdfXml vXml(sFile);
	vXml.Load();
	
	/* Root's Child */
	pElem = vXml.GetChild(NULL);
	if(pElem){
		/* Generater */
		Edf_GenerC		* eGener;
		std::map<string, string>	options;
		string	option;

		Edf_Prog		* eProg; 	
		string 			  nFile;
		const char		* cFile;
		char			* cDelim;
		cFile  = sFile.c_str();
		cDelim = (char *)strstr(cFile, ".xml");
		*cDelim='\0';
		nFile = cFile;

		eProg  = new Edf_Prog("./", nFile);

		Edf_XmlRd_Iter(vXml, pElem, eProg, NULL);

		
		eGener = new Edf_GenerC(eProg, options, option);
		eGener->generator_init();
		EdfProg_Gen(eProg, eGener);
		eGener->generator_fini();
		return 0;
	}
	printf("Xml Parse Error.\n");
	return -1;
}

#include <string.h>
int		main(int   argc, char   *argv[])
{
#if 0
	return EdfXml_Parse(string("coral.xml"));
#else
	if(argc < 2){
		printf("Usage:edf_gen ???.xml\n");
		return -1;
	}
	if(strstr(argv[1], ".xml") == NULL){
		printf("Usage:edf_gen ???.xml\n");
		return -1;
	}
	string  sFile = argv[1];
	return EdfXml_Parse(sFile);
#endif
}
